Android onSaveInstanceState() 和 onRestoreInstanceState() 什么时候被调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20831826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:32:57  来源:igfitidea点击:

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

androidandroid-activityrestoreandroid-lifecycle

提问by Luis Mendo

The following figure (from the official doc) describes the well-known lifecycleof an Android activity:

下图(来自官方文档)描述了众所周知的 Android Activity生命周期

enter image description here

在此处输入图片说明

On the other hand, when the activity is destroyed by the system (for example because memory needs to be reclaimed), the state of the activity is sometimes automatically saved and restoredby means of the methods onSaveInstanceState()and onRestoreInstanceState(), as illustrated by the following figure (also from the official doc):

另一方面,当Activity被系统销毁时(例如因为内存需要回收),Activity的状态有时会通过方法和方法自动保存和恢复,如下图所示(也来自官方文档):onSaveInstanceState()onRestoreInstanceState()

enter image description here

在此处输入图片说明

I'm aware that onSaveInstanceState()is not always calledwhen an activity is about to be destroyed. For example, if it is destroyed because the user has pressed the "back" button, the activity state is not preserved. But in the cases when the state issaved and restored, and onSaveInstanceState()/ onRestoreInstanceState()get called, when exactly are they called?

我知道当一个活动即将被销毁时,onSaveInstanceState()并不总是被调用。例如,如果它因为用户按下了“返回”按钮而被销毁,则不会保留活动状态。但是在状态保存和恢复以及onSaveInstanceState()/onRestoreInstanceState()被调用的情况下,它们究竟是什么时候被调用的

For example, according to the above figures, onRestoreInstanceState()might be called before onStart(), or after onStart()but before onResume(), or after onResume(). Similarly, several possibilities exist for onSaveInstanceState(). So when are they called exactly?

例如,根据上图,onRestoreInstanceState()可能被称为 before onStart(),或 afteronStart()但 before onResume(),或 after onResume()。同样,对于 存在几种可能性onSaveInstanceState()。那么它们究竟是什么时候被调用的呢?

Ideally, what I would like is to see a combined diagram showing the activity lifecycle states and the save/restore methods, if that exists.

理想情况下,我希望看到显示活动生命周期状态和保存/恢复方法(如果存在)的组合图

回答by Steve M

Per the documentation:

根据文档

void onRestoreInstanceState (Bundle savedInstanceState)

This method is called between onStart()and onPostCreate(Bundle).

void onSaveInstanceState (Bundle outState)

If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will occur before or after onPause().

void onRestoreInstanceState (Bundle savedInstanceState)

此方法在onStart()和之间调用onPostCreate(Bundle)

void onSaveInstanceState (Bundle outState)

如果调用,此方法将在 onStop() 之后针对以 Build.VERSION_CODES.P 开头的平台为目标的应用程序发生。对于面向早期平台版本的应用程序,此方法将在 onStop() 之前发生,并且无法保证它是在 onPause() 之前还是之后发生。

回答by Android Developer

As per doc1and doc2

根据doc1doc2

onSaveInstanceState

Prior to Honeycomb, activities were not considered killable until after they had been paused, meaning that onSaveInstanceState() was called immediately before onPause(). Beginning with Honeycomb, however, Activities are considered to be killable only after they have been stopped, meaning that onSaveInstanceState() will now be called before onStop() instead of immediately before onPause().

onRestoreInstanceState

This method is called between onStart() and onPostCreate(Bundle) when the activity is being re-initialized from a previously saved state

保存实例状态

在 Honeycomb 之前,活动直到被暂停后才被认为是可杀死的,这意味着 onSaveInstanceState() 在 onPause() 之前被立即调用。然而,从 Honeycomb 开始,只有在它们被停止之后,Activity 才被认为是可终止的,这意味着 onSaveInstanceState() 现在将在 onStop() 之前而不是在 onPause() 之前被调用。

onRestoreInstanceState

当活动从先前保存的状态重新初始化时,在 onStart() 和 onPostCreate(Bundle) 之间调用此方法

回答by azizbekian

In addition to already posted answers, there is a subtle change introduced in Android P, which is:

除了已经发布的答案之外,Android P 中还引入了一个微妙的变化,即:

void onSaveInstanceState(Bundle outState)

If called, this method will occur AFTERonStop()for applications targeting platforms starting with P. For applications targeting earlier platform versions this method will occur before onStop()and there are no guarantees about whether it will occur before or after onPause().

void onSaveInstanceState(Bundle outState)

如果调用,此方法将在针对以P开头的平台的应用程序之后发生。对于面向早期平台版本的应用程序,此方法将在 之前发生,并且无法保证它是在 之前还是之后发生。onStop()onStop()onPause()

Source: docs

来源:文档

As to why this change is introduced, here's the answer:

至于为什么要引入这个变化,答案如下:

... so an application may safely perform fragment transactions in onStop()and will be able to save persistent state later.

...因此应用程序可以安全地执行片段事务,onStop()并且可以稍后保存持久状态。

Source: docs

来源:文档

回答by Mahmoud Mzz

This is an extra information for onSaveInstanceState(Bundle)

这是onSaveInstanceState(Bundle)的额外信息

from docs

来自文档

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

不要将此方法与活动生命周期回调混淆,例如 onPause(),它总是在 Activity 被放置在后台或正在销毁的途中被调用,或者 onStop() 在销毁之前被调用。调用 onPause() 和 onStop() 而不是调用此方法的一个示例是当用户从活动 B 导航回活动 A 时:无需在 B 上调用 onSaveInstanceState(Bundle) 因为该特定实例永远不会被恢复,所以系统避免调用它。调用 onPause() 而不是 onSaveInstanceState(Bundle) 的一个例子是当活动 B 在活动 A 之前启动时:如果活动 A 在活动 A 的生命周期内没有被杀死,系统可能会避免在活动 A 上调用 onSaveInstanceState(Bundle) A 的用户界面的状态将保持不变。

So it's default implementation for..

所以它是默认实现...

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.

默认实现通过在层次结构中具有 id 的每个视图上调用 onSaveInstanceState() 并保存当前聚焦视图的 id(所有这些都由onRestoreInstanceState(Bundle) 的默认实现)。如果您重写此方法以保存每个单独视图未捕获的其他信息,您可能希望调用默认实现,否则准备好自己保存每个视图的所有状态。

回答by parvez rafi

String activityState;
@Override 
public void onCreate(Bundle savedInstanceState) {
// call the super class onCreate to complete the creation of activity like 
// the view hierarchy 
super.onCreate(savedInstanceState);

// recovering the instance state 
if (savedInstanceState != null) {
     activityState = savedInstanceState.getString(STATE_KEY);
 } 

   setContentView(R.layout.main_activity);
   mTextView = (TextView) findViewById(R.id.text_view);
} 

//This callback is called only when there is a saved instance previously saved using //onSaveInstanceState(). We restore some state in onCreate() while we can optionally restore // other state here, possibly usable after onStart() has completed. // The savedInstanceState Bundle is same as the one used in onCreate().

//仅当存在先前使用 //onSaveInstanceState() 保存的已保存实例时才会调用此回调。我们在 onCreate() 中恢复一些状态,同时我们可以选择在此处恢复 // 其他状态,可能在 onStart() 完成后可用。// saveInstanceState Bundle 与 onCreate() 中使用的相同。

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) {
 mTextView.setText(savedInstanceState.getString(STATE_KEY));
  } 


// invoked when the activity may be temporarily destroyed, save the instance 
//state here 
//this method will be called before onstop

@Override 
 public void onSaveInstanceState(Bundle outState) {
    outState.putString(STATE_KEY, activityState);

    // call superclass to save any view hierarchy 
    super.onSaveInstanceState(outState);
}