onSaveInstanceState(Bundle) is not called

2,738 views
Skip to first unread message

Masarano Itay

unread,
Dec 6, 2010, 5:38:20 AM12/6/10
to android-platform
onSaveInstanceState is not called before onDestroy() . (before
application is destroyed)

Does anyone have an answer ?

Thanks.

Dianne Hackborn

unread,
Dec 6, 2010, 1:22:07 PM12/6/10
to android-...@googlegroups.com
If you call finish() (or your activity is being finished for any other reason), it won't be called because the user will never be able to return to the activity so it will never need to restore its state.


--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

itay masarano

unread,
Dec 7, 2010, 6:41:03 AM12/7/10
to android-...@googlegroups.com
Thanks Dianne,

But according to documentation of Android it's written:

"This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state"

in Addition , if the Activity is finish for some reason , how can we save the Bundle so next time we can restore the data in the onCreate()?
Itay Masarano

ABTHUL RAZEETH

unread,
Dec 8, 2010, 12:27:53 AM12/8/10
to android-...@googlegroups.com
If the activity is beiing killed by the framework to reclaim the memory, then onSaveInstanceState will be called to store the state of the app. Here in your case, you are finishing the app yourself purposely, so there is no need for the framework to remember your state of the app, you should do it from the app itself, for example in case of message composer, you may ask the user to save the typed contents to drafts before finishing.
 
-
abthul

With Regards
Abthul Razeeth

itay masarano

unread,
Dec 8, 2010, 4:01:26 PM12/8/10
to android-...@googlegroups.com
But if the user press on the "back" button the app call the onDestory() and i want to save the state before, how can i achieve it ?

Nelson To

unread,
Dec 8, 2010, 4:09:17 PM12/8/10
to android-...@googlegroups.com

Do it in onpause().

On Dec 8, 2010 1:01 PM, "itay masarano" <imas...@gmail.com> wrote:

But if the user press on the "back" button the app call the onDestory() and i want to save the state before, how can i achieve it ?



On Wed, Dec 8, 2010 at 7:27 AM, ABTHUL RAZEETH <abthul...@gmail.com> wrote:
>

> If the activit...


--
You received this message because you are subscribed to the Google Groups "android-platform" gr...

itay masarano

unread,
Dec 8, 2010, 4:44:25 PM12/8/10
to android-...@googlegroups.com
But the Bundle is not in argument of the onPause()?

How can i tell android to save the bundle?


You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.



--
Itay Masarano

Dianne Hackborn

unread,
Dec 8, 2010, 7:11:03 PM12/8/10
to android-...@googlegroups.com
On Wed, Dec 8, 2010 at 1:44 PM, itay masarano <imas...@gmail.com> wrote:
But the Bundle is not in argument of the onPause()?
How can i tell android to save the bundle?

There would be no point in putting anything in the Bundle, because the activity is *finished* so its saved state bundle will never be used again.

Think of finish as "close" and the saved state bundle as some RAM you have to page out if your app's memory needs to be used for other things while you are in the background.  When the user closes/finishes your activity, none of its RAM is retained.  If you want to keep state across such interactions, it needs to go in persistent storage of your choosing.

Bryce Covert

unread,
Dec 9, 2010, 10:09:39 PM12/9/10
to android-...@googlegroups.com
Think of the bundle in onSaveInstanceState as a place to put data that would be needed to recreate an activity. As it turns out, activities are destroyed and recreated quite frequently -- even when you might not expect it, but an activity is only 'finished' once. 

One example of this is when the phone is rotated. At this time, the activity is destroyed and then a new one is created. Android will give you an opportunity to save data from the activity to pass to this new instance. This same sort of behavior happens when the system is running low on memory. Imagine you have four activities on the history stack: A -> B -> C -> D. Maybe by the time you hit activity D, the system's running low on memory. At this point, dalvik might destroy A, calling onSaveInstanceState on it. If you hit back from D, then to C, then to B, then to A again, it will recreate the activity from the data you stored in the bundle. 

In contrast, an activity is 'finished' when it will never be reshown. For example, if the user hits the back button from that activity.

Hope that helps,
Bryce


--

itay masarano

unread,
Dec 10, 2010, 1:29:34 AM12/10/10
to android-...@googlegroups.com
Thanks, very helpful.

So, if activity is actually finish, then we need to save the information in the SD Card before it destroyed in order to use that information in the future when application is started again,right ?

Is there any conventional way for saving application data in the SD , where to place the file,which file type(xml,etc)?

10x
--
Itay Masarano

Nelson To

unread,
Dec 10, 2010, 1:17:33 PM12/10/10
to android-...@googlegroups.com
SharedPreference!!

Dianne Hackborn

unread,
Dec 10, 2010, 5:56:43 PM12/10/10
to android-...@googlegroups.com
A lot of this is covered in the SDK documentation.




Also, this is all just general Android SDK programming material, so you will find a lot of discussion on it in the SDK forums like StackOverflow or android-developers.  Further questions in this area should likewise be directed to one of those forums.

itay masarano

unread,
Dec 11, 2010, 4:21:10 AM12/11/10
to android-...@googlegroups.com
OK, Thanks a lot.

itay masarano

unread,
Dec 13, 2010, 2:05:12 AM12/13/10
to android-...@googlegroups.com
Hi again Dianne,

Refer to the onSaveInstanceState , I read again the android documentation about this function (http://developer.android.com/reference/android/app/Activity.html) and it's contradiction to your explanation.


you wrote:

"If you call finish() (or your activity is being finished for any other reason), it won't be called because the user will never be able to return to the activity so it will never need to restore its state."

As i understand from you we won't have the chance to restore the Bundle in the next onCreate (after application was killed).


Android documentation:

"Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both). "


Can you explain please?
--
Itay Masarano

Dianne Hackborn

unread,
Dec 13, 2010, 3:49:31 AM12/13/10
to android-...@googlegroups.com
Hm I'm not sure how to explain this another way.

finished != "being killed".

If you are finished, the activity will never come back, so there is no reason to save its instance state (because there is no way in the world it would ever be needed).  If its process is killed but the user could still return to the activity, hopefully you had saved its instance state before that or else you will not be able to re-create the activity in the correct state.

John Vidler

unread,
Dec 13, 2010, 4:36:09 AM12/13/10
to android-...@googlegroups.com

Hi,

The documentation is correct because in the case of a finish() call, there is no app to refer to an 'instance' to restore said '...per-instance state...' to anymore.

For clarity; 'killed' in your (Itay's) example refers to when the memory management code garbage collects the process as an attempt to provide more memory for running or newly launching processes.

As such, this is analogous to it being 'paged out' in a traditional OS, and could mean the process will be called back to the foreground. (In this analogy, the data being paged out would be your Bundle object)

When the 'finish' method is run, it is in an instance where the app is being 'closed', rather than backgrounded, and any normal final functions should be performed (such as saving any state to non-volatile memory) as would be the case of a normally terminating process in a traditional OS.

Hope that helps!

- John.

[On the move, responses may be delayed]

On 13 Dec 2010 08:50, "Dianne Hackborn" <hac...@android.com> wrote:
> Hm I'm not sure how to explain this another way.
>
> finished != "being killed".
>
> If you are finished, the activity will never come back, so there is no
> reason to save its instance state (because there is no way in the world it
> would ever be needed). If its process is killed but the user could still
> return to the activity, hopefully you had saved its instance state before
> that or else you will not be able to re-create the activity in the correct
> state.
>
> On Sun, Dec 12, 2010 at 11:05 PM, itay masarano <imas...@gmail.com> wrote:
>
>> Hi again Dianne,
>>
>> Refer to the onSaveInstanceState , I read again the android documentation
>> about this function (
>> http://developer.android.com/reference/android/app/Activity.html) and it's
>> contradiction to your explanation.
>>
>>
>> you wrote:
>>
>> "If you call finish() (or your activity is being finished for any other
>> reason), it won't be called because the user will never be able to return to
>> the activity so it will never need to restore its state."
>>
>> As i understand from you we won't have the chance to restore the Bundle in
>> the next onCreate (after application was killed).
>>
>>
>> *Android documentation:*
>>
>> "Called to retrieve per-instance state from an activity *before being
>> killed* so that the state can be restored in onCreate(Bundle)<http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29>or
>> onRestoreInstanceState(Bundle)<http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29>(the
>> Bundle <http://developer.android.com/reference/android/os/Bundle.html>populated by this method will be passed to both). "

>>>>>>> .
>>>>>>> For more options, visit this group at
>>>>>>> http://groups.google.com/group/android-platform?hl=en.
>>>>>>>
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "android-platform" group.
>>>>>> To post to this group, send email to android-...@googlegroups.com
>>>>>> .
>>>>>> To unsubscribe from this group, send email to

>>>>>> .
>>>>>> For more options, visit this group at
>>>>>> http://groups.google.com/group/android-platform?hl=en.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Itay Masarano
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "android-platform" group.
>>>>> To post to this group, send email to android-...@googlegroups.com.
>>>>> To unsubscribe from this group, send email to

>>>>> .
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/android-platform?hl=en.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Dianne Hackborn
>>>> Android framework engineer
>>>> hac...@android.com
>>>>
>>>> Note: please don't send private questions to me, as I don't have time to
>>>> provide private support, and so won't reply to such e-mails. All such
>>>> questions should be posted on public forums, where I and others can see and
>>>> answer them.
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google Groups
>>>> "android-platform" group.
>>>> To post to this group, send email to android-...@googlegroups.com.
>>>> To unsubscribe from this group, send email to

>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/android-platform?hl=en.
>>>>
>>>
>>>
>>>
>>> --
>>> Itay Masarano
>>>
>>
>>
>>
>> --
>> Itay Masarano
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "android-platform" group.
>> To post to this group, send email to android-...@googlegroups.com.
>> To unsubscribe from this group, send email to
Reply all
Reply to author
Forward
0 new messages