on coming back to fragment ArrayList getting empty (restore ArrayList)

久未见 提交于 2019-12-12 01:44:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!