7

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();
Rami
  • 7,879
  • 12
  • 36
  • 66
Camus
  • 195
  • 2
  • 7

6 Answers6

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
    }
    ...
}
Larry McKenzie
  • 3,253
  • 25
  • 22
0

try to use "replace' instead of 'add'

part of the code

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

wennnt
  • 41
  • 11
  • 1
    If you use replace, then current fragment will be replaced with new fragment, so when you click back, you won't go back to the previous fragment because it was already replaced! – Kishan Solanki May 07 '20 at 08:05
0

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

user3009752
  • 164
  • 2
  • 9
  • 24
0

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.

Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
  • 1
    and if we again add the fragment what happens then?? Does the previous one remains active?? If after killing the new fragment can we go back to previous fragment where we left the fragment? – Shubham Agarwal Bhewanewala Sep 10 '17 at 07:09
0

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"
-1

Try to use

manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

before adding new FragmentTransaction See here

pRaNaY
  • 24,642
  • 24
  • 96
  • 146