In my open source Android app, an issue has been discovered where a specific fragment will come into view on top of another fragment or crash the app in a certain situation.
The issue on GitHub if you want to see more information and example screenshots: https://github.com/rpi-mobile/RPIMobile-Android/issues/31
I have pinned down the reason, but want to know which of the methods in the android.support.v4.app package to use to resolve the issue.
In MainActivity.java
, is the code for the navigation drawer that uses FragmentTransaction.replace()
to switch fragments.
The issue arises because in MapFragment
, I use:
ViewMapFragment vmf = new ViewMapFragment();
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content_frame, vmf);
ft.commit();
And in ViewMapFragment
's onDestroyView()
:
FragmentManager fm = getSherlockActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fm.findFragmentById(R.id.mapview));
ft.commit();
The onDestroyView()
properly removes the ViewMapFragment
from view, but if you have it in view and use the navigation drawer to change to a different fragment, MapFragment
is still on the back stack.
So, for my questions:
1) How do I check if a particular fragment is on the back stack before attempting to remove/replace it, or will the app not crash if you attempt when nothing is there (i.e. not checking)? E.g. calling popBackStack()
when there is nothing on the back stack.
2) Should I use FragmentManager
class methods to attempt to remove the MapFragment
from the back stack or should I use FragmentTransaction
methods? Pros vs cons?
3) What is the difference in the UI between popBackStack()
and popBackStackImmediate()
? Does the user see some glitchy transitions?