0

I have two fragments in a ViewPager while in the first one I have added two more child fragments and these two child fragments have views and asynchronous calls in them. Now when I swipe from one fragment of viewPager to other and then come back on the first then all views remains there in the child fragment.

I want the child fragments' views and asynchronous calls to be null or destroyed when swipe from first fragment of viewpager to other just to avoid memory leaks. Everything is working fine.

I have added two fragments in first view pager fragment like below:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.strongholdapps.scannertr.tablayout.Fragments.FirstFragment">

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:name="com.strongholdapps.scannertr.tablayout.Fragments.FirstCurrencyFragment"
    android:id="@+id/firstCurrency"/>

<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:name="com.strongholdapps.scannertr.tablayout.Fragments.FirstCurrencyFragment"
    android:id="@+id/secondCurrency"/>
</LinearLayout>
Ali Khan
  • 61
  • 2
  • 10
  • What asynchronous methods are you using? You may call the stop or cancel method on your asynchronous call `onPause()` of `fragments`. – Tenten Ponce Jan 11 '18 at 05:17
  • I'm using Retrofit calls. Normally I'm successfully canceling calls and destroy views in activities. But @TentenPonce how can I destroy views in the scenario that I mentioned. – Ali Khan Jan 11 '18 at 05:42
  • Well, don't worry about leaks because you're cancelling the calls. Destroying the activities specially you're on a `ViewPager` will just slow your app because it will rebuild again the time the user goes back to it. Let the android manage it. – Tenten Ponce Jan 11 '18 at 05:47
  • hmm. Sounds good. Ok I'll handover this to android, let it manage. Hope it'll not create memory leaks. Thanks @TentenPonce – Ali Khan Jan 11 '18 at 06:23

0 Answers0