I'm developing a simple fragment-based application with a singe FragmentActivity. Each 'screen' of the application is contained in a fragment and all fragments are added to the container layout at application startup.
// Set up fragments in Main Activity
fragMan = getFragmentManager();
FragmentTransaction ft = fragMan.beginTransaction();
ft.add(R.id.fragment_container, settingsFragment);
ft.add(R.id.fragment_container, mapFragment);
ft.add(R.id.fragment_container, tracksFragment);
ft.add(R.id.fragment_container, waypointsFragment);
ft.commit();
Transitions are accomplished by hiding the currently visible fragment, then showing the appropriate fragment.
ft = fragMan.beginTransaction();
ft.show(mapFragment);
ft.addToBackStack(null);
ft.commit();
This all works fine, but when the back button is pressed, the application exits, regardless of which screen is visible or what previous transactions have been added to the back stack.
I've checked to make sure that the back stack is properly accumulating records and tried many different variations of transition methods such as replacing fragments rather than hiding/showing them, creating new instances of fragments rather than storing them in variables, etc. As far as I can tell, my code matches all the tutorials and examples I can find, and I haven't even been able to find any similar questions/examples of similar problems, presumably because the standard implementation 'just works' for others.
I suspect it may be a problem at the application level such as a property in my manifest (which I've investigated pretty thoroughly) or something inherent to the way my application is set up which prevents the back button from functioning properly. I can override onBackPressed to handle the transitions manually, but this seems like a very ugly workaround. Any ideas as to why this might not be behaving as expected? By the way this is on a Nexus 7 running Jelly Bean.
Check if you are using FragmentActivity(from support library) instead of Activity. This will cause backstack and transition problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With