I have a TabAdapter class in my activation, which, on one of the tabs (with index 0) displays a fragment, GroupList. The TabAdapter itself is created in onCreate, activation. By the button the list is filled with data. Everything works, everything is OK, until you turn the phone. In a coup, it performs the same onCreate, but something goes wrong. The fragment that tries to fill the button, the list adapter is null. For debugging purposes, I logged information about creating a GroupList and triggering its life cycle (the adapter is initialized to the onCreate fragment). What happened can be seen on the screen (note the id of the objects). In the blue frame part of the log, after turning the screen.
1 answer
You need to use the onSaveInstanceState
and onRestoreInstanceState
to save data when you rotate the screen and restore it after the rotation. Or you can try to just keep the fragment in memory (using setRetainInstance(true)
), and use it again after turning the screen.
The explanation (the answer to the question "Who created this fragment?") Is that when you restore activity after a rotation, it will automatically restore the fragments that were added to it, so adding a new fragment in your activity will add another new fragment fragments (or fragments) that have already been restored.
That is why you have in the logs and appears after turning the screen two times recording
GroupList GroupList ()
UPD
In order to get to the fragment in the TabAdapter'а
constructor, you can use tags. For example, when creating a fragment, add the tag:
groupList = new GroupList(); getFragmentManager().beginTransaction() .add(R.id.your_placeholder, groupList, "YOUR_TAG") .commit();
where your_placeholder
is the id of the widget in the place of which the fragment should be displayed. And then access to the fragment can be obtained through the method findFragmentByTag()
.
- The fact is that in this particular case I would be completely satisfied with creating everything anew, which I tried to achieve. - Sergey Vodakov
- oneplease post your code - Ksenia
- github.com/WaterSmith/testRotateScreen - Sergey Vodakov
- and where do you see the output of the "Return GroupList" log? - Ksenia
- While examples prepared, accidentally overwritten. This output should be in the TabAdapter, in the getItem method: Log.i (TAG, "return" + groupList); Already fixed - Sergey Vodakov