2

In my activity, I have various fragments. By default activity displays a map. On listitem click, fragment A, B or C is displayed using following code:

protected void replaceFragment(int i) {
    FragmentManager fragmentManager = getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    switch (i) {
    case FRAGMENT_A:
        aFragment = new AFragment ();
        fragmentTransaction.replace(R.id.main_framelayout_replace,
                aFragment , TAG_A_FRAGMENT);
        fragmentTransaction.commit();
        break;//and so on.....
     default:
        break;
    }
}

Here I'm facing an issue: When I replace FragA with FragB which is nested fragment i.e. it contains list and detail fragment within itself. When I try to remove any fragment other than FragB, I'm able to do it successfully and show the default map screen but when I'm on FragB and try to remove it, I'm not able to see default map screen. Instead a blank white screen is getting displayed.

Removing of fragments is done as follows:

if (aFragment != null) {
                        fragmentManager.beginTransaction()
                                .remove(aFragment ).commit();
                    }//and so on...

For FragB which is having list and detailed fragment, I also do following in onDetach of FragB,

fragmentManager.beginTransaction()
                    .remove(MainActivity.listFragment).commit();

Am I doing anything wrong here? Any help appreciated.

Note: I'm not getting any exception in any try catch. All the lines of code are getting executed without error including onDetach of FragB.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
GAMA
  • 5,958
  • 14
  • 79
  • 126

1 Answers1

0

In your FragB class (which extends Fragment) you should be using ChildFragmentManager to manage nested fragments.

TeasingDart
  • 371
  • 1
  • 6
  • Tried with that also, didn't worked though... Still the same blank screen issue. – GAMA Jul 30 '15 at 05:51
  • Your code snippets are a bit too sparse. I can't tell if you are calling both replace() and remove() on FragA. You don't need to explicitly remove FragA, FragB, or FragC if you are calling replace(). The only fragment(s) you need to explicitly remove are the child fragments of FragB. – TeasingDart Jul 30 '15 at 05:56
  • Whenever I need to switch between fragments, I use replace. When I need to navigate to main screen ,I use remove. But FragB being nested fragment, I'm also calling remove method for child fragments in onDetach of FragB in addition to calling remove for FragB itself. – GAMA Jul 30 '15 at 05:59
  • Make sure you are creating AFragment() only once. I think you may be creating duplicates. I create the base classes for all the fragments once during Activity.onCreate() then reuse them in FragmentTransaction.replace() as needed. – TeasingDart Jul 30 '15 at 06:00
  • Fair point. I moved `aFragment = new AFragment ();` and equivalent from `replaceFragment()` to `onCreate()` of MainActivity. But still, same issue. – GAMA Jul 30 '15 at 06:08
  • Try adding `fragmentTransaction.show(aFragment);` after replace(). – TeasingDart Jul 30 '15 at 06:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84641/discussion-between-teasingdart-and-gama). – TeasingDart Jul 30 '15 at 06:13