I have a main Activity with a Fragment layout. The drawer has 3 option:
Fragment[1], Fragment[2], Fragment[3].
Inside Fragment[2] and Fragment[3] is one button. This button open other fragment. Fragment[4].
I want Fragment[4] without drawer but with a back button.
This is the onClick code in Fragment[2]
Fragment fragment = new InstalacionesEncontradasFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("key", this.instalacionesConCategorias);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.replace(R.id.main_frame_container, fragment, "ACTIVIDADES").commit();
And in Fragment[4]
onCreate method:
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
But this solution doesn't work.
How to disable the drawer? Where should I implement the back button? In Fragment[2] or Fragment[3]?
Calling setRetainInstance(true) will prevent the Fragment from being re-created. But, right now the Activity is always re-creating the DrawerFragment and forcefully re-creating the HomeFragment , resulting in the behavior you are seeing. In your Activity. onCreate() check the savedInstanceState is null .
To detach an added Fragment from an Activity, you use: getFragmentManager(). beginTransaction(). detach(mFragment). commit().
Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.
You can use :
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
This will lock drawer opening on swipe
Add the line
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
in Activity which makes all fragments like Fragment 1, 2,3 and 4. May be in your case, Fragment 4 is from differenct activity than Fragment 2. So, back button press is not working
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