I have 3 fragments, Frag_A, Frag_B & Frag_C.
My navigation logic is: Frag_A ==> Frag_B ==> Frag_C . That's Frag_A is added into layout firstly, so, at the first time, Frag_A is shown on screen, then, if user press the Next Button, Frag_A is replaced by Frag_B, now Frag_B is shown on screen, if user press the Next button, Frag_B is replaced by Frag_C, so that Frag_C is shown on screen.
Everything works fine with my code at this point.
Method to change to next fragment:
//here, argument 'fragment' is either Frag_B or Frag_C
public void showFragment(Fragment fragment){
FragmentManager fragMgr = activity.getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();
fragTrans.replace(R.id.frag_placeholder, fragment, fragmentName);
fragTrans.addToBackStack(null);
int transId = fragTrans.commit();
fragMgr.executePendingTransactions();
}
Layout file of activity (Frag_A is added to the frag_placeholder when Activity starts):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/frag_placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</merge>
But, after Frag_C is shown, if I pop Frag_C by press the physical back button and invoke the showFragment(Frag_C)
method to show the Frag_C again right after my code detected the physical button is pressed, I get a blank page shown on screen. Why & how to fix it?
================Explain=====================
I said above "*show the Frag_C again right after my code detected the physical button is pressed*", here is what I mean:
In Activity:
@Override
public void onBackPressed() {
super.onBackPressed();
//I detect the Physical Back button is pressed & invoke the method to show the popped Frag_C again.
}
Based on @ Luksprog 's comment, I found that if I create a new instance of Frag_C
instead of always use the same instance, the blank page problem get fixed.
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