Nested fragments survive screen rotation

Posted by ievgen on Stack Overflow See other posts from Stack Overflow or by ievgen
Published on 2014-06-11T21:21:03Z Indexed on 2014/06/11 21:24 UTC
Read the original article Hit count: 165

I've faced with an issue with Nested Fragments in Android. When I rotate the screen the Nested Fragments survive somehow. I've come up with a sample example to illustrate this issue.

public class ParentFragment extends BaseFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_parent, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

         getChildFragmentManager()
                 .beginTransaction()
                 .add(getId(), new ParentFragmentChild(), ParentFragmentChild.class.getName())
                 .commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        log.verbose("onResume(), numChildFragments: " + getChildFragmentManager().getFragments().size());
    }     
}

public class ParentFragmentChild extends BaseFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_child, null);
    }
}

BaseFragment just logs method calls. This is what I see when I rotate the screen.

When Activity initially appears

ParentFragment? onAttach(): ParentFragment{420d0a98 #0 id=0x7f060064}
ParentFragment? onCreate()
ParentFragment? onViewCreated()
ParentFragmentChild? onAttach(): ParentFragmentChild{420d08d0 #0 id=0x7f060064 com.kinoteatr.ua.filmgoer.test.ParentFragmentChild}
ParentFragmentChild? onCreate()
ParentFragmentChild? onViewCreated()
ParentFragment? onResume()
ParentFragment? onResume(), numChildFragments: 1
ParentFragmentChild? onResume()

Screen rotation #1

ParentFragmentChild? onPause()
ParentFragment? onPause()
ParentFragment? onSaveInstanceState()
ParentFragmentChild? onSaveInstanceState()
ParentFragmentChild? onStop()
ParentFragment? onStop()
ParentFragmentChild? onDestroyView()
ParentFragment? onDestroyView()
ParentFragmentChild? onDestroy()
ParentFragmentChild? onDetach()
ParentFragment? onDestroy()
ParentFragment? onDetach()
ParentFragment? onAttach(): ParentFragment{4211bc38 #0 id=0x7f060064}
ParentFragment? onCreate()
ParentFragmentChild? onAttach(): ParentFragmentChild{420f4180 #0 id=0x7f060064 com.kinoteatr.ua.filmgoer.test.ParentFragmentChild}
ParentFragmentChild? onCreate()
ParentFragment? onViewCreated()
ParentFragmentChild? onViewCreated()
ParentFragmentChild? onAttach(): ParentFragmentChild{42132a08 #1 id=0x7f060064 com.kinoteatr.ua.filmgoer.test.ParentFragmentChild}
ParentFragmentChild? onCreate()
ParentFragmentChild? onViewCreated()
ParentFragment? onResume()
ParentFragment? onResume(), numChildFragments: 2
ParentFragmentChild? onResume()
ParentFragmentChild? onResume()

Screen rotation #2

ParentFragmentChild? onPause()
ParentFragmentChild? onPause()
ParentFragment? onPause()
ParentFragment? onSaveInstanceState()
ParentFragmentChild? onSaveInstanceState()
ParentFragmentChild? onSaveInstanceState()
ParentFragmentChild? onStop()
ParentFragmentChild? onStop()
ParentFragment? onStop()
ParentFragmentChild? onDestroyView()
ParentFragmentChild? onDestroyView()
ParentFragment? onDestroyView()
ParentFragmentChild? onDestroy()
ParentFragmentChild? onDetach()
ParentFragmentChild? onDestroy()
ParentFragmentChild? onDetach()
ParentFragment? onDestroy()
ParentFragment? onDetach()
ParentFragment? onAttach(): ParentFragment{42122a48 #0 id=0x7f060064}
ParentFragment? onCreate()
ParentFragmentChild? onAttach(): ParentFragmentChild{420ffd48 #0 id=0x7f060064 com.kinoteatr.ua.filmgoer.test.ParentFragmentChild}
ParentFragmentChild? onCreate()
ParentFragmentChild? onAttach(): ParentFragmentChild{420fffa0 #1 id=0x7f060064 com.kinoteatr.ua.filmgoer.test.ParentFragmentChild}
ParentFragmentChild? onCreate()
ParentFragment? onViewCreated()
ParentFragmentChild? onViewCreated()
ParentFragmentChild? onViewCreated()
ParentFragmentChild? onAttach(): ParentFragmentChild{42101488 #2 id=0x7f060064 com.kinoteatr.ua.filmgoer.test.ParentFragmentChild}
ParentFragmentChild? onCreate()
ParentFragmentChild? onViewCreated()
ParentFragment? onResume()
ParentFragment? onResume(), numChildFragments: 3
ParentFragmentChild? onResume()
ParentFragmentChild? onResume()
ParentFragmentChild? onResume()

They keep getting multiplied. Does anybody know why is that ?

© Stack Overflow or respective owner

Related posts about android

Related posts about android-fragments