I
I
Igor-Novikov2020-01-31 15:13:13
Android
Igor-Novikov, 2020-01-31 15:13:13

Why is the onDetach method called when returning to the previous fragment in the fragment stack?

There are 2 fragments A and B.
Fragment B can be added to the fragment stack by the following method.

Fragment b = new B();
  getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.new_fragment_container, b).commit();

Fragment B has a back button. When clicked, the code is executed.
FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0)
            fm.popBackStack();
        else
            finish();


After executing this code, the onDetach() method of fragment B is executed, then fragment A is taken from the stack.
Why does the onDetach() method of this fragment A also work when it is taken from the stack?

The order in which fragment A's methods are called after it has been popped off the stack.
onCreateView()
onAtach()
onDestroyView()
ondestroy()
onDetach()
onCreateView()
onStart()
onResume()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question