I've got an Android application which is working fine, but I've found an annoying bug which I don't know what else do to solve it.
My application has a single Activity (call it HomeActivity; well, plus the Preferences Activity) and lots of Fragments. HomeActivity manages the replacement of every Fragment using this method (found in this thread and especially looking into the following comment):
public void switchFragment(Fragment pFragment, String pTagFragment){
String backStateName = pTagFragment;
boolean fragmentPopped = mFragmentManager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.content_frame, pFragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
To replace a Fragment, what I do is the following:
switchFragment(new FragmentEuskalmet(), FragmentEuskalmet.FRAGMENT_NAME);
Here it's the problem: I navigate through several Fragments (say, 3 of them). Then, I wanna go back, so I press back 2 times. When I arrive to the last within the stack, a white screen is shown (i.e. white is shown in the place where the Fragment should be). The odd thing is that if I do a long press to my home button to show the recent apps and I choose again my application, the fragment gets loaded correctly.
I assume there might be something within my switchFragment
method, e.g. it may be any issue related to the backStack. One important detail is that I've got written the so called empty constructor in every Fragment.
Can anybody lend me a hand? I can provide further code pieces if anybody request them.
Edit: I've considered compulsory to type my sss method:
@Override
public void onBackPressed(){
invalidateOptionsMenu();
if(CURRENT_FRAGMENT != FragmentMapa.FRAGMENT_NAME){//"com.ingartek.bizkaimove.ui.FragmentMapa"){
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerRightRelativeLayout);
}
if(mDrawer.isDrawerOpen(GravityCompat.START)){
mDrawer.closeDrawer(GravityCompat.START);
setToolbarSubtitle(mSubtitleAux);
}else{
setToolbarSubtitle(getString(R.string.app_name_subtitle));
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
//finish();
showExitDialog();
}else {
super.onBackPressed();
popFragment();
}
}
}
I think I should rewrite this part:
}else {
super.onBackPressed();
popFragment();
}
Solution: As @Ajay Pandya suggested, I leave the onBackPressed() that way:
@Override
public void onBackPressed(){
invalidateOptionsMenu();
if(CURRENT_FRAGMENT != FragmentMapa.FRAGMENT_NAME){//"com.ingartek.bizkaimove.ui.FragmentMapa"){
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerRightRelativeLayout);
}
if(mDrawer.isDrawerOpen(GravityCompat.START)){
mDrawer.closeDrawer(GravityCompat.START);
setToolbarSubtitle(mSubtitleAux);
}else{
setToolbarSubtitle(getString(R.string.app_name_subtitle));
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
//finish();
showExitDialog();
}else{
mFragmentManager.popBackStackImmediate();
}
} else {
super.onBackPressed();
}
}
}
You can manage back stack and screen for exit like
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
This is because you are adding all your fragments programmatically. On pressing back buttons, you eventually remove the first fragment that you added as well.
The reason you get it back on launching the application is because you add the fragment again.
I would suggest overwrite your back button. Whenever you pop fragment, check to see that it's not your first fragment and then pop 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