Android: fragments overlapping issue

前端 未结 17 1320
闹比i
闹比i 2020-11-27 17:49

I am facing a problem of overlapping fragments when i switch between tabs and attach fragments to a tab view below is my code please help

public class Fragme         


        
相关标签:
17条回答
  • 2020-11-27 18:07

    I don't think implementing a white background is a safe solution although it may be convenient. Basically the problem occurs because fragment manager is confused about which fragment to pop.

    backstack looks like this and thinks your on frag 4 1->2, 2->3, 3->4 but your actually on frag 6 for some reason so popbackstack goes

    remove(4) add(3) <- now you have 3(new) and 6(left behind) blended together

    To better control my navigation I keep track of my current/previous fragments and replace them like this. This allows my app to control fragment navigation.

    if ( !(currentFragment instanceof Settings)) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, settings);
            fragmentTransaction.addToBackStack(null);
            previousFrag = currentFragment;
            currentFragment = settings;
            fragmentTransaction.commit();
        }
    

    Secondly, I believe the fragmentmanager.replace() method is a better alternative. Not sure if that is available to OP at the time.

    Third, to handle the androids native back press you need to be able to scroll back indefinitely. Many people recommend not adding the first fragment to back stack by not using this line.

    fragmentTransaction.addToBackStack(null);

    However if that's the case you must check if its the first time the app has loaded. If its not and you exclude this line then your app will have these issues whenever you navigate through this fragment. I prefer to leave it in place and do the following instead.

    Not sure if this is considered a good solution but it works very well.

    1- get backstack count 2- remove all existing frags 3- keep track of you fragment objects 4- if its the last frag in the stack the user wishes to exit and since we added the main activity to the back stack we need to double pop

    @Override
    public void onBackPressed() {
    
    int num = getFragmentManager().getBackStackEntryCount();
    
    
            fl.removeAllViews();
            super.onBackPressed();
    
            if  (currentFragment != previousFrag) {
                currentFragment = previousFrag;
            }else{
                currentFragment = null;
            }
    
            if (num == 1){
               super.onBackPressed();
            }
    
    0 讨论(0)
  • 2020-11-27 18:12

    Well Setting up fragment background color is not a solution because fragment will be still in the activity stack which may consume memory.

    Solution would be remove all views from your framelayout before adding any new fragment.

    private void changeFragment(Fragment fr){
        FrameLayout fl = (FrameLayout) findViewById(R.id.mainframe);
        fl.removeAllViews();
        FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
        transaction1.add(R.id.mainframe, fr);
        transaction1.commit();
    }
    
    0 讨论(0)
  • 2020-11-27 18:12

    Another problem can be related to using android.R.id.content as a container. I've just created FrameLayout and use id from there.

    0 讨论(0)
  • 2020-11-27 18:14

    when you have a overlap fragment, maybe your background of your fragment is transparent, you need put a android:background="@color/white"' inside your fragment propeties

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    

    and white you need put inside of the colors.xml #FFFFFF in the rest folder.

    0 讨论(0)
  • 2020-11-27 18:14

    I found simple and clean solution. For me problem was in execution fragment transaction in every call of onCreate() of my Activity. Now I perform transaction only if savedInstanceState == null

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            if (savedInstanceState == null) {
            // replace fragment 
            }
        }
    
    0 讨论(0)
提交回复
热议问题