I'm creating an app, with one main activity and fragments. I'm trying to control what will happen on backPress using this code on the mainActivity:
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
this.finish();
} else {
getFragmentManager().popBackStack();
}
}
The structure of my app is this:
MainAtivity --> Fragment1 --> Fragment2 --> Fragment2 ... (back Press should get me to Fragment1)
When I created Fragment2 I added Fragment1 to the backStack, and when pressing back I still see Fragment2 in the background.
How can I get to Fragment1 and only Fragment1 to show?
I'm not sure about how do you commit your fragment. But this would helpful.
While commit:
While removing:
This may works !!!
Happy coding !!!!