I add Fragments
to my Activity
dynamically based on user interaction. When I press the back key, the fragments are popped. However when I press the back key for the fragment which was first added to the stack, the 'Activity' shows an empty layout. I would like the Activity to call `finish()' at this point and disappear. I've tried:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if(keyCode == KeyEvent.KEYCODE_BACK){
if(getFragmentManager().getBackStackEntryCount()==0){
finish();
return true;
}
}
return true;
}
But this has the effect of blocking the back key functionality. Any pointers in the right direction are appreciated.
Where are you adding your very first fragment? Don't add that transaction to the back stack it should work the way you want it.
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