'after add a fragment the previous fragment still visible

At first, I use the method replace to add fragments to back stack, then I found when I press back key,the fragment in stack will invoke the onCreateView again, I also found this behavious in api demos, so I think it is not a bug, but I would like achieve the effect like activity's behave that when I press the back key the previous activity would not invoke the onCreate method.

Later I found fragmentManager.add() could achieve my idea, but another probrolem appear, when add the second fragment, the previous fragment still visiable.

Could anyone help me?

FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction2 = manager.beginTransaction();
        transaction2.add(R.id.fl, f2);
        transaction2.addToBackStack("Fragment2");
        transaction2.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction2.commit();


Solution 1:[1]

Got back to using replace and just add a check to see if the bundle is null.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ...
    if (savedInstanceState == null) {
        // do work 
        // If the view is being recreated this code won't run
    }
    ...
}

Solution 2:[2]

try to use "replace' instead of 'add'

part of the code

fragmenttransaction.replace(R.id.realtabcontent, Fragment); fragmenttransaction.commit();

Solution 3:[3]

Check if you have set an argb background for your fragments. In that case replace that background with the rgb. The a means transparent.

Solution 4:[4]

If you are adding fragment for first time you can add the fragment like in your code.

Fragment_Home fragment = new Fragment_Home();
        FragmentTransaction fts = (MainActivity.this).getSupportFragmentManager().beginTransaction();
        fts.add(R.id.frame_container, fragment);
//        fts.addToBackStack(fragment.getClass().getSimpleName());
        fts.commit();

If you want to move to other fragment from this then you should replace current fragment with new one.

Fragment_CreateGroup fragment = new Fragment_CreateGroup();
            fts = (MainActivity.this).getSupportFragmentManager().beginTransaction();
//            fts.addToBackStack(fragment.getClass().getSimpleName());
            fts.replace(R.id.frame_container, fragment);
            fts.commit();

Here fts is object of FragmentTransaction defined on top.

Solution 5:[5]

to fix this issue you just need to change background inside the root in xml and put that code for avoid clicked from previous fragment.

android:background="@drawable/background"
android:focusable="true"
android:clickable="true"

Solution 6:[6]

Try to use

manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

before adding new FragmentTransaction See here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Larry McKenzie
Solution 2
Solution 3 user3009752
Solution 4 Anuj Sharma
Solution 5 Mohamed Mohamed Taha
Solution 6 pRaNaY