How to save an activity's state?
This question already has an answer here:
You need to store your shopping cart data to persistent storage somewhere. Take a look at the storage option documentation that someone else mentioned.
Basically, you need to save your cart item to the file system somewhere (sqlite db, text file, shared prefs). You'll do this when your activity exits onStop()
or onDestroy()
then retrieve the saved cart item in your activity onCreate()
or onResume()
or whichever method you are using to populate your cart.
The other way to do this is to use intent extras to pass your item data between activities by using intent.putExtra(name, value)
. This works fine as long as your activities don't get killed, in which case the data will be lost. So, it's safer to save to persistent storage.
上一篇: 我如何保存我的活动状态?
下一篇: 如何保存活动的状态?