How to destroy views or asynchronous calls in a child fragment of a view Pager fragment Android

154 Views Asked by At

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>
0

There are 0 best solutions below