How to save activity state after pressing back button?

馋奶兔 提交于 2020-01-25 07:04:26

问题


I have 2 activities A and B first one having products list and second one is the shopping cart. On click of a button in the list I am adding products to the cart.

Now I go to the cart and delete some items from it. I press back and came to the activity A again. Now if I go to the B(cart) again the list is null i.e there are no products in the cart.

I am using onSaveInstanceState and onRestoreInstanceState to save an restore the instance of cart activity but that is not working. Perhaps this is happening because I clicked the back button which destroyed cart activity and these methods were never called. I referred to this question: Saving activity state on Android and this Activity state after pressing back button. On pressing of back button activity is destroyed and popped out of the stack so I maybe its the case that I cannot use onSaveInstanceState and onRestoreInstanceState.

What I should use to save the products in my cart activity even if I press back button and how can I retrieve that on recreation of the activity? During some research I got to know I can use shared preferences, or SQLite database to store my activity state but I didn't found a good implementation. Any help regarding this is appreciated. Please share any useful SO links or tutorials regarding the same.

来源:https://stackoverflow.com/questions/34526717/how-to-save-activity-state-after-pressing-back-button

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