Previous fragment visible below new fragment

后端 未结 2 1023
南笙
南笙 2021-01-20 02:09

I have a TabLayout with a ViewPager. The ViewPager have four fragments F1, F2, F3 and F4. F1 contains a FrameLayout which can have 2 fragments F11 and F12. Initially I add F

2条回答
  •  不要未来只要你来
    2021-01-20 02:21

    I have faced the same problem. But I also could not find a proper solution for this. In my scenario when I open multiple apps and them come back to my app it causes the same issue with Fragments that happen on your side. But I made my app stable. I have done this by the following solution:

    1. When your app remove from memory then again open the app then onCreate() method called of your MainActivity in which you are showing Fragments.

    2. Here I checked the backstackEntrycount and if it is greater then 0, then remove all fragments and show the base fragment.

      // Clear all fragments and show base fragment, So it will not create
      // issue after opening
      // multiple apps
      FrameLayout flSliding = (FrameLayout) findViewById(R.id.fl_sliding);
      flSliding.post(new Runnable() {
      
          @Override
          public void run() {
              FragmentManager fragmentManager = getSupportFragmentManager();
              Log.i("NavigationDrawerActivity",
                      "onCreate " + fragmentManager.getBackStackEntryCount());
              if (fragmentManager.getBackStackEntryCount() > 0) {
                  removeFragment(fragmentManager.getBackStackEntryCount());
              }
              Log.i("NavigationDrawerActivity",
                      "onCreate " + fragmentManager.getBackStackEntryCount());
          }
      });
      

    Hope it will help!

提交回复
热议问题