Issue
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?
Solution
I'm not sure about how do you commit your fragment. But this would helpful.
While commit:
// Initialize a Fragment Transaction.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Record all steps for the transaction.
ft.replace(R.id.home_frame_layout_for_fragments, baseFragments[i],
// Add the transaction to backStack with tag of first added fragment
ft.addToBackStack(baseFragments[0].getTagText());
// Commit the transaction.
ft.commit();
While removing:
final FragmentManager fragmentManager = getSupportFragmentManager();
while (fragmentManager.getBackStackEntryCount() != 0) {
fragmentManager.popBackStackImmediate();
}
This may works !!!
Happy coding !!!!
Answered By - chain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.