Asghar Hosseini
Asghar Hosseini

Reputation: 297

Reload Fragment after popBackStack

enter image description hereI need reload fragment after this process Go from Fragment A to Fragment B and go from Fragment B to Fragment C, then do the registration process, and after the registration is correct, popBackStack Fragment A and my Fragment A will be reloaded. My main problem is that Fragment A is not reloaded Please help me

Upvotes: 0

Views: 1353

Answers (1)

Mobin Yardim
Mobin Yardim

Reputation: 1024

you can use navigation component library for solve this problem!

this will be your nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/AFragment">


    <fragment
        android:id="@+id/AFragment"
        android:name="ir.inbo.navigationComponenetBug.AFragment"
        android:label="AFragment"
        tools:layout="@layout/fragment_a">

        <argument
            android:name="someLong"
            app:argType="long" />
        <action
            android:id="@+id/action_AFragment_to_BFragment"
            app:destination="@id/BFragment" />
    </fragment>
    <fragment
        android:id="@+id/BFragment"
        android:name="ir.inbo.navigationComponenetBug.BFragment"
        android:label="BFragment"
        tools:layout="@layout/fragment_b">

        <argument
            android:name="someLong"
            app:argType="long" />
        <action
            android:id="@+id/action_BFragment_to_CFragment"
            app:destination="@id/CFragment" />
    </fragment>
    <fragment
        android:id="@+id/CFragment"
        android:name="ir.inbo.navigationComponenetBug.CFragment"
        android:label="CFragment"
        tools:layout="@layout/fragment_c" >

        <argument
            android:name="someLong"
            app:argType="long" />
        <action
            android:id="@+id/action_CFragment_to_AFragment"
            app:destination="@id/AFragment"
            app:popUpTo="@id/AFragment"
            app:popUpToInclusive="true" />
    </fragment>
</navigation>

and in AFragment you must move your logic to onViewCreated or onCreateView because of the fragment life cycle when you navigate from AFragment to Bfragment, Afragment's view will be destroyed and when you come back to Afragment from Cfragment, onViewCreated and onViewCreate will be called

and this is google example for your exact use case

Upvotes: 1

Related Questions