back-stack
Fragment methods: attach(), detach(), remove(), replace(), popBackStack()
Now when we call remove() on the fragment from our activity, which functions of the fragment are called and in which order? Look at http://developer.android.com/reference/android/app/Fragment.html . The order is: onPause(), onStop(), onDestroyView(), onDestroy(), onDetach() What does attach() and detach() do? Does detach() remove the fragment? And when these two attach() and detach() are used, which … Read more
How to replace fragment C with fragment A when back button is pressed?
You need to do 2 things – name the FragmentTransaction from A->B and then override onBackPressed() in your containing activity to call FragmentManager#popBackStack (String name, int flags) when you are on Fragment C. Example: Transition from A->B getSupportFragmentManager() .beginTransaction() .replace(R.id.container, new FragmentB(), “FragmentB”) .addToBackStack(“A_B_TAG”) .commit(); Transition from B->C will use a similar transaction with “FragmentC” … Read more
savedInstanceState when restoring fragment from back stack
onSaveInstanceState is (unfortunately) not called in normal back-stack re-creation of a fragment. Check out http://developer.android.com/guide/components/fragments.html#Creating and the answer on How can I maintain fragment state when added to the back stack?
Android: Fragments backStack
If you really want to replace the fragment then use replace() methode instead of doing a remove() and an add(). FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(…………..); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); Don’t forget to do the addToBackStack(null) so your previous state will be added to the backstack allowing you to go back with the back … Read more
Android – save/restore fragment state
When a fragment is moved to the backstack, it isn’t destroyed. All the instance variables remain there. So this is the place to save your data. In onActivityCreated you check the following conditions: Is the bundle != null? If yes, that’s where the data is saved (probably orientation change). Is there data saved in instance … Read more
How to get a list of backstack fragment entries in android?
The FragmentManager has methods: getBackStackEntryCount() getBackStackEntryAt (int index) FragmentManager fm = getFragmentManager(); for(int entry = 0; entry<fm.getBackStackEntryCount(); entry++){ Log.i(TAG, “Found fragment: ” + fm.getBackStackEntryAt(entry).getId()); }
How to clear the Android Stack of activities?
This should be bitwise OR’d or you end up overwriting the earlier flag. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Like so: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Navigating back to FragmentPagerAdapter -> fragments are empty
I had the same problem. The solution for me was simple: in onCreateView I had: // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity() .getSupportFragmentManager()); where SectionPageAdapter is something like this: class SectionsPagerAdapter extends FragmentPagerAdapter { … } after changing … Read more