当系统调用onPause
和onStop
活动方法生命周期时,我有一个困惑。我看到通过从我的活动导航到另一个应用程序,此处系统正在调用onPause
和onStop
,当我再次启动我的应用程序的另一个活动时,系统正在调用onPause
和{{1方法
onStop
这里answer说" onPause()调用当一个Activity将控件转移到另一个Activity时,onStop()方法调用当Activity在完成模式时。"我觉得错误的是,当我开始另一个活动时,两个人都在打电话,我只是开始活动而没有完成,但仍然@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, SecondActivity.class));
}
和onPause
方法正在调用。
Doc说"如果您的活动不长,则会拨打
onStop
"在这里我可以调用onStop
当您的活动不再可见并被其他活动隐藏但仍然onStop
也在调用时会出现混乱但是为什么?
答案 0 :(得分:1)
为了简单起见,这里是来自android developers documentation的小信息。
Activity Lifecycle
Activities in the system are managed as an activity stack. When a new activity is
started, it is placed on the top of the stack and becomes the running activity --
the previous activity always remains below it in the stack, and will not come to the
foreground again until the new activity exits.
An activity has essentially four states:
If an activity in the foreground of the screen (at the top of the stack), it is active or running.
If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
请参阅this生命周期演示应用程序。这对理解活动生命周期非常有帮助。
同样如Raghunandan所建议的那样,你必须阅读this。
<强>加了:强>
例如:当用户在事件发生后从活动A遍历活动B(FullScreen Non Transparent)时
因此,在onPause中,如果需要,您可以保存活动状态或其他一些有用的信息。
祝你好运:)