Home > Blockchain >  Reload Fragment after popBackStack
Reload Fragment after popBackStack

Time:01-03

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

CodePudding user response:

What you're trying to achieve is actually a skip operation while you're accessing the backStack.

There are two approaches to this

  1. Give a tag to each of your fragments like so
transaction.add(R.id.main_activity_container, FirstFragment, "FirstFragment");
transaction.replace(R.id.main_activity_container, Second, "SECOND");
transaction.replace(R.id.main_activity_container, Third, "THIRD");

and then override the onBackPressed to check that if the user is in the third fragment on pressing the back button should go to the first one.

@Override
public void onBackPressed() {
if (getSupportFragmentManager().getFragments().get(lastStack).getTag().equalsIgnoreCase("THIRD")){
//fragment transaction to go to the first screen 
}
}
  1. Pushing a null to the back stack like so .addToBackStack(null) on the 2nd fragment and then pop normally from the third fragment

CodePudding user response:

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 you exact use case

  • Related