Back Button Behaviour On Activity To Fragment In Bottom Navigation View
I have a bottomnavigationview containing 5 fragments,(Consider A(Home), B, C, D, E) If I go to fragment D and then an Activity (Which is inside fragment D or C or any of 5) and pre
Solution 1:
NOTE PLEASE USE!!! ADD FRAGMENT INSTEAD OF REPLACE FRAGMENT AS YOLO said in the answer you can get the stack count and use it in the favour of your algorithm
Well I don't that this is the best way to do the thing that you like! but still it can help you . in your mainActivity where your bottom navigation is ! have that global declared and initialised ! and do something like this in onResume method ! you can empty or check the stack of fragments using !
if (getSupportFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
do something like this in your onResume! pass out desired item number(#position) in the getItem of bottom navigation!
@Override
protected void onResume() {
super.onResume();
// here you can check the stack that last fragment in stack is the one that//opened the 2nd activity then you can use its fragment to be loaded in //transaction and also highlight the the bottomnav's tab
bottomNavigationView.getMenu().getItem(0).setChecked(true);
}
Solution 2:
Replace your code with following:
FragmentTransactiontransaction= getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fadein, R.anim.fadeout);
transaction.replace(R.id.home_container, fragment);
transaction.addToBackStack(fragmentName)
transaction.commit();
Post a Comment for "Back Button Behaviour On Activity To Fragment In Bottom Navigation View"