Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

android - Are onCreate and onRestoreInstanceState mutually exclusive?

I have a couple of question regarding onRestoreInstanceState and onSaveInstanceState.

1) where do these methods fit the activity lifecycle? I have read a lot of documentation but there is no clear idea, except a broad statement that when the state of the activity is to be saved

2) Are onCreate and onRestoreInstanceState mutually exclusive?

3) is onRestoreInstanceState called when the activity is destroyed? what does this mean? an activity always destroyed except for scenarios when another activity is floating on top of current.

4) onRestoreInstanceState appears to be called only from instrumentation in jelly bean. Is this no longer relevant to activity lifecycle?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

where do these methods fit the activity lifecycle?

OnSaveInstanceState is called right before your activity is about to be killed or restarted (e.g b/c of memory pressure or configuration change). Note that this is different from onPause which gets called when your activity loses focus (e.g you transition to another activity).

Usually onSaveInstanceState will be called after onPause but before onStop but not always. E.g if you press back, then the activity is destroyed (like calling finish()) and there is no need to save state so onSaveInstanceState is not called. So why not just save state in onPause? Just because the activity loses focus doesn't mean it has been killed. It is still in memory. Basically you don't want to save state every time you are paused but rather when you are paused and about to become invisible (i.e go from foreground to background).

So what should you do in onPause? Ideally you should release resources that drain your battery e.g network connections, turn off geo or accelerometer, pause a video (all of this depends on your app). And restore these resources in onResume which, as you might have guessed, gets called when your activity gains focus.

Are onCreate and onRestoreInstanceState mutually exclusive?

onRestoreInstanceState is redundant because you can easily restore state in onCreate.

Having said that here is what the official doc says for onRestoreInstanceState:

Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation.

So for best practice, lay out your view hierarchy in onCreate and restore the previous state in onRestoreInstanceState. If you do that, anyone who subclasses your Activity can chose to override your onRestoreInstanceState to augment or replace your restore state logic. This is a long way of saying onRestoreInstanceState serves as a template method.

is onRestoreInstanceState called when the activity is destroyed? what does this mean?

This was partially answered in 1. Yes, onRestore gets called when the system is about to destroy your activity. The system will destroy your activity when it is under memory pressure or the user explicitly closes the application (e.g swipe-delete from recents in nav bar) or there is a configuration change (e.g landspace to portrait).

Why is android designed like this (unlike desktop apps)? Because on mobile systems, resource management is an acute problem b/c of battery life. So you want to provide hooks into app lifecyle so that the app can cleanly save and restore their state between shutdowns or losing focus while at the same time making it totally transparant to the user.

onRestoreInstanceState appears to be called only from instrumentation in jelly bean. Is this no longer relevant to activity lifecycle?

I don't understand this question. Can you rephrase it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...