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.