android - Save and restore instance state of an Activity manually -
i have download activity in app, users can download content. activity called main activity, , retrieves list of available files download on network.
to avoid fetching whole list again when download activity recreated e.g. because of rotation, override onsaveinstancestate() store list in bundle , evaluate bundle passed oncreate() method. works rotation.
however, able implement same behavior if user hits , later returns download activity. android in case not call onsaveinstancestate(), hence nothing gets saved.
my idea override onbackpressed() , include call onsaveinstancestate().
- will work @ all?
- can
onsaveinstancestate(new bundle()), or have initialize new bundle somehow? - will android store bundle , pass
oncreate()next instance of activity creates, or have take care myself of storingbundle, passing new instance (if so, how)?
update:
simply adding
@override public void onbackpressed() { onsaveinstancestate(new bundle()); super.onbackpressed(); } gives me following exception:
java.lang.illegalstateexception: can not perform action after onsaveinstancestate
for call onbackpressed(). (swapping 2 lines nothing, presumably because there nothing left save.)
will work @ all?
no.
will android store bundle , pass oncreate() next instance of activity creates
no.
do have take care myself of storing bundle , passing new instance (if so, how)?
no.
store model data in file or database, process-level cache (e.g., custom singleton) minimize disk i/o. load data out of cache available, or reload disk if needed.
Comments
Post a Comment