clarification about Android documentation -


note: if system destroys activity while it's stopped, still retains state

of view objects (such text in edittext) in bundle (a blob of key-value pairs) , restores them if user navigates the same instance of activity (the next lesson talks more using bundle save other state data in case activity destroyed , recreated).

the same instance of activity

how exact instance when destroyed , recreated , isn't going new memory block (another instance ), me clear point ?

unfortunately documentation isn't clear in lot of areas. 1 of them.

in theory, if android destroy activity while stopped, call ondestroy() , gc reclaim memory. in actual fact, android never destroys individual activities free memory. kills entire os process instead. in case, ondestroy() never gets called on of activities in process. gc doesn't bother clean anything, because vm (virtual machine) dies along else in process , entire amount of memory in use process reclaimed operating system.

when user navigates application, android creates new process application , create new instance of activity. in case will, of course, new block of memory instance. see constructor called , oncreate() called. however, since android saves state of activity's views, state restored activity call super.oncreate().

there few cases android destroy instance of activity , create new 1 automatically. done, example, during configuration (ie: orientation) change. in case, android calls ondestroy() on old instance , creates new instance of activity. new instance gets saved state of old instance , can therefore restore state of views able. in case, since new instance being created, will, of course, have different address in memory.

once component destroyed, dead , memory using can reclaimed gc. android never reanimate dead instance.

hopefully clarifies situation you.

edit add more details

android keeps track of tasks , activities in tasks in own internal data structures. each activity, keeps copy of intent started activity, recent intent sent activity , keeps bundle containing saved state of activity. android calls onsaveinstancestate() on activity give activity chance save need restore activity if android decides kill it. default implementation of onsaveinstancestate() saves state of activity's views. implementation needs save else activity need restore if killed , recreated android (for whatever reason). activity's private member variables , static variables not automatically saved , restored in bundle, if need these able recreate activity must save them using bundle provided onsaveinstancestate(). static variables stay around lifetime of application's process, since android can kill process (to reclaim resources) @ time, without warning, cannot rely on static variables having expected values (in case android has killed , recreated process).

if activity (or process) killed android , user later navigates activity, android uses information has in internal data structures create new instance of activity. calls oncreate() on new instance passing saved instance state bundle parameter. can used restore activity state had before original instance killed. android call onrestoreinstancestate() after calling onstart() can restoration of state in method if don't want in oncreate().

note: remember want save/restore in bundle must either primitive (int, boolean, etc.) or must implement serializable or parcelable.

the documentation of onsaveinstancestate() contains useful information well.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -