OnDetach is called when the fragment

I am having a problem with displaying the same fragment after changing orientation. I put it in the back and popping back. It jumps to onCreateView, etc., but then it calls onDetach, which causes the wrong fragment to be displayed .. the code looks like this:

    //fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {...}

    @Override
public void onDetach() {
    super.onDetach();
    this.getActivity().getFragmentManager().beginTransaction().addToBackStack(null);
}

and basically

    if (getFragmentManager().getBackStackEntryCount()>1){
        setContentView(R.layout.activity_main);
        getFragmentManager().popBackStack();
    }

Am I missing something? Thank you for your help in advance.

+4
source share
1 answer

Whenever you change orientation, your activity is recreated, and therefore the fragment placed inside your activity is recreated, and your fragments all life cycle methods are called again.

, , : http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

+2

Source: https://habr.com/ru/post/1568951/


All Articles