2

I have MainActivity

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, AFragment.newInstance(75))
                .commit();
    }

    @Override
    public void onBackPressed() {
       if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
           getSupportFragmentManager().popBackStack();
       } else {
           super.onBackPressed();
       }
    }

Also, I have AFragment

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        view.findViewById(R.id.show_b_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();

                appCompatActivity.getSupportFragmentManager()
                        .beginTransaction()
                        .add(R.id.fragment_container, BFragment.newInstance())
                        .addToBackStack("B")
                        .commit();
            }
        });

        return view;
    }

BFragment is just an empty fragment.

And I have the following fragments lifecycle output:

Open the App:
A - onCreateView
A - onStart
A - onResume

Click R.id.show_b_btn on AFragment:
B - onStart
B - onResume

Click back button:
B - onPause
B - onStop

Close the App:
A - onPause
A - onStop

So, the question is why AFragment doesn't call onPause when I click ShowBFragment and why AFragment doesn't call onResume when I click back button?

I think it happens because I use .add() and BFragment just overlays AFragment, so AFragment also "lives" when BFragment is visible, am I right?

Serhii K.
  • 639
  • 1
  • 8
  • 17
  • Yes, you are right. as you use use .add() and BFragment just overlays AFragment, so AFragment also "lives" when BFragment is visible – Rivu Chakraborty Nov 04 '16 at 10:03

2 Answers2

4

Just replace

getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, AFragment.newInstance(75))
                .commit();

with

getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, AFragment.newInstance(75))
                .commit();

This is what will happen if you use FragmentTransaction.add on a container. Your added fragment will be placed on top of your existing fragment. If you use FragmentTransaction.replace(R.id.container,fragment) it will remove any fragments that are already in the container and add your new one to the same container

you can also refer below link

Difference between add(), replace(), and addToBackStack()

Community
  • 1
  • 1
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • If I'll do it, AFragment will call onResume and onPause. I don't need it. I've been interested why AFragment doesn't do it when I use .add(). – Serhii K. Nov 04 '16 at 10:13
  • This is what will happen if you use FragmentTransaction.add on a container. Your added fragment will be placed on top of your existing fragment. If you use FragmentTransaction.replace(R.id.container,fragment) it will remove any fragments that are already in the container and add your new one to the same container – Jitesh Mohite Nov 04 '16 at 10:16
  • can you please up the answer – Jitesh Mohite Nov 04 '16 at 11:06
  • sorry, I can't do it. I have < 15 reputation. – Serhii K. Nov 04 '16 at 12:58
0

1) fragmentTransaction.addToBackStack(str);

Description - Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

2) fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag)

Description - Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

3) fragmentTransaction.add(int containerViewId, Fragment fragment, String tag)

Description - Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity. 1) fragmentTransaction.addToBackStack(str);

Description - Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

2) fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag)

Description - Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

3) fragmentTransaction.add(int containerViewId, Fragment fragment, String tag)

Description - Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

What does it mean to replace an already existing fragment, and adding a fragment to the activity state and adding an activity to the back stack ?

There is a stack in which all the activities in the running state are kept. Fragments belong to the activity. So you can add them to embed them in a activity.

You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. This is essentially useful when you have defined your fragment container at different layouts. You just need to replace with any other fragment in any layout.

When you navigate to the current layout, you have the id of that container to replace it with the fragment you want.

You can also go back to the previous fragment in the backStack with the popBackStack() method. For that you need to add that fragment in the stack using addToBackStack() and then commit() to reflect. This is in reverse order with the current on top.

Sahil Bansal
  • 609
  • 8
  • 6