Removing a Fragment from the Back Stack

Removing a Fragment from the back stack

You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods:

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();

How to remove Fragments from backstack

You could try this

 FragmentManager fm = getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.group,startFrag,"");
ft.addtoBackStack("startFrag");
ft.commit();

enter code here

// add other 4 fragments here

when you want to go startFrag on click of some button you can try below code.
fm.popBackStack("startFrag",0);
//where startFrag is the tag which you specify when you called
//addtoBackStack("startFrag")

Clear back stack using fragments

I posted something similar here

From Joachim's answer, from Dianne Hackborn:

http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42

I ended up just using:

FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}

But could equally have used something like:

((AppCompatActivity)getContext()).getSupportFragmentManager().popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE)

Which will pop all states up to the named one. You can then just replace the fragment with what you want

Remove Fragments from Backstack using Navigation Component

popBackStack(R.id.firstFragment, true) pops everything up to and inclusive to the first fragment. You've popped every fragment off the back stack. Seems like you want to use false, not true if you just want to return to the first fragment.

Remove Fragment from backstack with Navigation components

You can do that programmatically from Kotlin code if you need some extra logic while doing pop up.
But if you just need to remove PinSetupFragment from your back stack, you can do that on your navigation graph xml file.

So, if you are just going to pop up your fragment without any other extra logic, best way to pop up PinSetupFragment from your back stack would be updating your navigation_graph.xml file.

Just add these two lines to your action:

app:popUpTo="@id/pinSetupFragment"
app:popUpToInclusive="true"

As a result, your navigation_graph.xml file will be like this:

<?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/pin_create_nav_graph"
app:startDestination="@id/pinSetupFragment">

<fragment
android:id="@+id/pinSetupFragment"
android:name="com.example.ui.fragments.pin.PinSetupFragment"
android:label="Create PIN"
tools:layout="@layout/fragment_pin_setup" >
<action
android:id="@+id/action_pinSetupFragment_to_pinCreateFragment"
app:destination="@id/pinCreateFragment"
app:popUpTo="@id/pinSetupFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/pinCreateFragment"
android:name="com.example.ui.fragments.pin.PinCreateFragment"
android:label="Your PIN"
tools:layout="@layout/fragment_pin_create" />
</navigation>


Related Topics



Leave a reply



Submit