6

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.
}
Mellon
  • 37,586
  • 78
  • 186
  • 264
  • *right after my code detected the physical button is pressed,* - what does this mean exactly? – user May 21 '13 at 07:30
  • Please check my explanation in OP – Mellon May 21 '13 at 07:35
  • A wild guess... do you use the `showFragment()` method with the same instance of the fragment? Also that seems a bit strange, the user removes the fragment only for you to show it again. – user May 21 '13 at 07:55
  • @ Luksprog, my code is a prove of concept of one of our product, I won't describe the reason why I do it, but this is the logic I need. But for your 1st question, yes, I am using the same instance of Frag_C – Mellon May 21 '13 at 08:00
  • Well instead of what you currently do(and I really don't understand that behavior) either use a new instance of that fragment or in the `onBackPressed()` simply cancel the event if you're at the target fragment(not sure it will work). – user May 21 '13 at 08:11
  • @ Luksprog, please put your comment in an answer, since after I used a new instance of the fragment(Frag_C), the blank page isn't shown anymore, though I got another error "Frag_C is not attached to Activity", I am wondering why also, but your suggestion did solve my current problem – Mellon May 21 '13 at 08:38

1 Answers1

1

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.

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • But creating new instance take more memory. – Arslan Anwar Apr 29 '14 at 14:14
  • I stumbled upon the same problem, apparently it is not fixed until now october 2017, and I want to avoid creating new fragments each time. Gonna investigate more into details, please share if you have any info – LPVOID Oct 21 '17 at 14:58