简体   繁体   English

活动生命周期?

[英]Activity lifecycle?

I think my ideas on activity lifecycle and bundles 我认为我对活动生命周期和捆绑的想法
are a little confused,can you help me? 有点困惑,你能帮助我吗?

Let's suppose the user opens activity A from home screen, 假设用户从主屏幕打开活动A
activity A "calls" activity B which fills the screen. 活动 “调用” 活动B ,填充屏幕。
On this event onSaveInstanceState() is called on activity A then onPause() and onStop() . 在此事件上, onSaveInstanceState()活动A上调用,然后在onPause()onStop()
Since there are too many apps currently running on the system, 由于系统上当前运行的应用程序太多,
andorid decides to kill the process hosting activity A . andorid决定杀死托管活动A的进程。

When the user navigates back to activity A ,onCreate() is called an we can 当用户导航回活动A时 ,我们可以调用onCreate()
use the bundle ( setted during the last call of onSaveInstaceStae() ) to restore the state. 使用bundle(在onSaveInstaceStae()的最后一次调用期间设置)来恢复状态。 then onStart() , onRestoreInsanceState() 然后onStart()onRestoreInsanceState()
and onResume() are called, onResume()被调用,
am I right? 我对吗?

Then lets suppose the user presses back key to exit from activity A 然后让我们假设用户按下后退键以退出活动A.
onPause() , onStop() and onDestory() are called in sequence on activity A (the call of onDestroy() could be postponed though) onSaveInsanceState() should not be called in this scenario. onPause()onStop()onDestory()活动A上按顺序调用onDestroy()虽然可以推迟onDestroy()的调用onDestroy()在这种情况下不应该调用onSaveInsanceState()

When the user opens again activity A later on then the bundle 当用户稍后再打开活动A然后打开捆绑包
passed to onCreate() is null,right? 传递给onCreate()是null,对吗?

Now Suppose the user rotates screen onSaveInsanceState() , OnPause() , OnStop() , OnDestroy() are called 现在假设用户旋转屏幕onSaveInsanceState()OnPause()OnStop()OnDestroy()被调用
then onCreate() with bundle setted by the last call to onSaveInsanceState() , 然后onCreate()使用最后一次调用onSaveInsanceState()设置的bundle,
and then onStart(), and onRestore(). 然后是onStart()和onRestore()。 am I right? 我对吗?

My guess is that: 我的猜测是:

when the user creates an ativity,the bundle passed to onCreate() is always null and onRestoreState() is never called,but when the system creates it , for instance when it killed the activity because of low memory or because of a rotation event,the bundle passed is the one setted by the last call of onSaveInstanceState(). 当用户创建一个活动时,传递给onCreate()的bundle总是为null,并且永远不会调用onRestoreState() ,但是当系统创建它时,例如当它由于内存不足或者因为旋转事件而杀死活动时,传递的bundle是最后一次调用onSaveInstanceState()设置的bundle。

Is my guess right? 我猜对了吗?

thanks and sorry for my poor english. 谢谢,抱歉我的英语不好。

PS : I think onRestoreInstanceState() is passed the same bundle is passed onCreate() but typically state is restored using onCreate() . PS:我认为onRestoreInstanceState()传递相同的bundle传递onCreate()但通常使用onCreate()恢复状态。

onRestoreInstanceState()传递相同的bundle传递onCreate()是正确的,并且系统通过调用onCreate()重启活动并且还调用onRestoreInstanceState(),如果从活动开始时从onCreate()获取,则bundle将为null 。

Since there are too many apps currently running on the system, andorid decides to kill the process hosting activity A. 由于系统上当前运行的应用程序太多,因此andorid决定终止进程托管活动A.

This is a very common situation. 这是一种非常普遍的情况。 Furthermore - you can emulate this action using developers option. 此外 - 您可以使用开发人员选项模拟此操作。

在此输入图像描述

In this case each activity, that was move into background, will be automatically destroyed. 在这种情况下,将进入后台的每个活动将自动销毁。

About Bundle in OnCreate . 关于OnCreate的Bundle。

I can get from my memory only two cases when OnCreate will be called with non-null Bundle. 当使用非null Bundle调用OnCreate时,我只能从我的记忆中得到两种情况。 First - described above. 首先 - 如上所述。 Second case - screen rotation. 第二种情况 - 屏幕旋转。

When you launch app Android calles 当您启动应用Android calles时

  • onCreate 的onCreate
  • onStart 在onStart
  • onResume 的onResume

After that lets doing screen rotation. 之后让屏幕旋转。 Android will call Android会打电话

  • onSaveInstanceState 的onSaveInstanceState
  • onPause 在onPause
  • onStop 的onStop
  • onDestroy 的onDestroy
  • onCreate 的onCreate
  • onStart 在onStart
  • onRestoreInstanceState onRestoreInstanceState
  • onResume 的onResume

The more information you can find on topic about recreating 有关重新创建的主题,您可以找到的信息越多

Interesting question - never thought about it. 有趣的问题 - 从未考虑过。

Take a look at the documentation, onCreate() and onSaveInstanceState() . 看一下文档onCreate()onSaveInstanceState()

This answers at least your question what Bundle will be supplied to onCreate() . 这至少回答了你的问题Bundle将被提供给onCreate()

Unfortunately there is no exact definition on which events onSaveInstanceState() is called, but I guess it is called by default in all relevant situations (whatever they may be...), but you can find out for some situations (eg rotating the screen) by putting a Log.i() to LogCat. 不幸的是,没有关于调用onSaveInstanceState()事件的确切定义,但我猜它在所有相关情况下默认调用(无论它们是什么......),但你可以找到某些情况(例如旋转屏幕) )通过将Log.i()放到LogCat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM