Android - Save activity state when starting new activity
I'm currently working an application that should have the following behavior : In one of the activities, the user can select different items into a list. While doing that, he should be able to take a picture, save it, and return to the previous activity.
I'm having the following problem : when the user goes back to the activity after taking the picture, the progress (basically, the items selected) is lost.
I tried to use android:launchMode="singleInstance" (as well as singleTask) and android:alwaysRetainTaskState="true" on my activity to save its current state (and it works great when the user leave the application to do something else, then come back later to it). I tried to lunch the ACTION_IMAGE_CAPTURE intent with the FLAG_ACTIVITY_NEW_TASK to simulate the same behavior (somewhere in my mind i thought it should be the same thing as lunching the picture activity from the homescreen), but with no effect. I even tried to lunch the picture activity with startActivityforResult with the hope that somehow my activity wouldn't be lost, but with still no effect.
I know I could use the Bundle savedInstanceState to manually save and restore the state by overriding onPause, onResume, on Create (etc), but there is actually lots of data in custom components that are not easily serializable, so I'd like to avoid that.
Well I guess my question is how can I reproduce the same behavior as alwaysRetainTaskState manifest attribute? Or is there anyway to easily save the state of my activity? Or what am I doing wrong, missing the obvious?
Thanks a lot开发者_如何学编程!
I know I could use the Bundle savedInstanceState to manually save and restore the state by overriding onPause, onResume, on Create (etc), but there is actually lots of data in custom components that are not easily serializable, so I'd like to avoid that.
What you would "like" does not much matter. You need this code anyway to handle configuration changes (e.g., switch from portrait to landscape). Please implement onSaveInstanceState()
and onRestoreInstanceState()
, then back out all the rest of the inappropriate hacks that you have in your previous paragraph (e.g., singleInstance
, alwaysRetainTaskState
).
精彩评论