问题
first onClick in Monday_fragment it adds the data to ArrayList. Second OnClick in Monday_fragment it takes to Monday_list_fragment where the list is shown. There is a button in Monday_list_fragment which brings it back to the Monday_fragment. I debugged an seen, when i came back all the data in ArrayList is gone. How to keep the ArrayList data not to clear out on coming back.
Codes
Monday_fragment https://github.com/tirthoguha/DroidProject/blob/myDiary/src/com/example/s0217980_diary/Monday_fragment.java
Monday_list_fragment https://github.com/tirthoguha/DroidProject/blob/myDiary/src/com/example/s0217980_diary/Monday_list_fragment.java
回答1:
When the fragment is started, it creates a new, empty list. In the first onClick
in Monday_fragment
, it replaces the list with one created from entryLogs
. When you return from Monday_list_fragment
, the onCreate
method is called, and the list and entryLogs are overwritten with new, empty ones.
The onCreate
method can be called multiple times, including when the device orientation changes. You need to rethink how and when you set up the state of your fragment.
回答2:
dude I am just continuing form the que. If you have done same as you posted code here, just paste this line in onCreateView
of Monday_fragment
.
myStringList= MainActivity.getMyStringList();
来源:https://stackoverflow.com/questions/23160112/on-coming-back-to-fragment-arraylist-getting-empty-restore-arraylist