Is this the right way to clean-up Fragment back stack when leaving a deeply nested stack?
Well there are a few ways to go about this depending on the intended behavior, but this link should give you all the best solutions and not surprisingly is from Dianne Hackborn
http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42
Essentially you have the following options
- Use a name for your initial back stack state and use
FragmentManager.popBackStack(String name,
.
FragmentManager.POP_BACK_STACK_INCLUSIVE) - Use
FragmentManager.getBackStackEntryCount()
/getBackStackEntryAt().getId()
to retrieve the ID of the first entry on the back stack, andFragmentManager.popBackStack(int id,
.
FragmentManager.POP_BACK_STACK_INCLUSIVE) FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
is supposed to pop the entire back stack... I think the documentation for
that is just wrong. (Actually I guess it just doesn't cover the case where
you pass inPOP_BACK_STACK_INCLUSIVE
),
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
Clear back stack when navigation drawer is opened
Your answer is here:
Is this the right way to clean-up Fragment back stack when leaving a deeply nested stack?
Essentially, what you want is getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
How to Remove All Previous Fragments from Current Fragment except current one and first one?
On destroy of the Fragment
F5
clear Back Stack
upto F2
.
Try something like this:
public
method in your MainActivity
:
public void clearBackStackInclusive(String tag) {
getSupportFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
now in your F5
fragment:
@Override
public void onDestroy() {
super.onDestroy();
((MainActivity)getActivity()).clearBackStackInclusive("tag"); // tag (addToBackStack tag) should be the same which was used while transacting the F2 fragment
}
Reference
Related Topics
How to Use Web Camera in Android Emulator to Capture a Live Image
System Is Returning Error 127 When Called from C++ in Linux
Jelly Bean Datepickerdialog --- How to Cancel
Android Listview Refresh Single Row
Starting an Activity from a Service After Home Button Pressed Without the 5 Seconds Delay
How to Make an App's Background Image Repeat
Android SQLite Db When to Close
Multi-Line Edittext with Done Action Button
End Call in Android Programmatically
How to Create Standard Borderless Buttons (Like in the Design Guideline Mentioned)
Android Studio Gradle Configuration with Name 'Default' Not Found
Using "Printf" for Hex Values in Android Shell
How to Marshall and Unmarshall a Parcelable to a Byte Array with Help of Parcel
How to Create Text File and Insert Data to That File on Android
Build and Install Unsigned APK on Device Without the Development Server
Android Sdk Asynctask Doinbackground Not Running (Subclass)
Android - How to Unregister a Receiver Created in the Manifest