Meghana M
Meghana M

Reputation: 535

Orientation change does not call onCreateView immediately after onAttach

I am having a parent fragment, and loading child fragments over it. So initially when I add fragments it follows proper life cycle events, like onAttach(), onCreateView() and so on. I am setting a member var in onCreateView and using it immediately after commit() from parent fragment. But when orientation changes, onAttach() gets called and gets crashed while accessing member var, as it gets null. Seems like onCreateView() does not get called immediately. Also tried getChildFragmentManager().executePendingTransactions() after commit, but did not work. Please help. Thanks in advance.

Upvotes: 0

Views: 652

Answers (1)

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

you have to use in your manifest file use this in parent activity in which your fragment is

<activity
            android:name=".YourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"/>

After that it will persist state upon rotate screen. Hope it helps you. Test it and do let me know if this solves your issue.

Upvotes: 1

Related Questions