最简单的Andr​​oid活动的生命周期最简单、生命周期、Andr、oid

2023-09-04 11:06:26 作者:为何拉钩还要上吊

我注意到,Android开发活动部分已更新,因为我开始我的应用程序,但我仍不清楚最简单的活动生命周期是什么。

I noticed that the Android Developers Activity section has been updated since I started my app, but I am still unclear what the simplest Activity Lifecycle is.

据我可以做出来:

的onCreate,onResume和的onPause是必不可少的。

onCreate, onResume and onPause are the essential ones.

活动可能会被删除的onPause之后的任何时间,所以我要我的整个应用程序状态保存到一个文件中的onPause,而不是依靠的onStop或的onDestroy。此外,的onSaveInstanceState不叫之前,每一个这样的onPause是不是真的值得使用。

The activity may be deleted any time after onPause, so I should save my whole app state to a file onPause and not rely on onStop or onDestroy. Also, onSaveInstanceState is not called before every onPause so is not really worth using.

而不是试图写code加载处理所有的情况下,为什么在它的onPause年底不破坏活动?

Rather than trying to write loads of code to handle all the scenarios, why not destroy the Activity at the end of its onPause?

生命周期将被的onCreate和onResume之前处于活动状态,然后在onPause当它变为无效。其他方法将不需要

The Lifecycle would then be onCreate and onResume before it is active, then onPause when it becomes inactive. Other methods would not be needed.

我会使用的onCreate调用的setContentView和设置视图的听众,但一切将放在onResume,包括从文件加载恢复状态? 如前所述,在onPause将状态保存到一个文件,并破坏活动。

I'd use onCreate to call setContentView and set up view listeners, but everything else would be put in onResume, including loading the restored state from a file? As stated earlier, onPause would save the state to a file and destroy the activity.

据我所看到的,这样做的唯一的缺点可能是,当一个弹出窗口在屏幕上,该活动将被删除,有当弹出窗口关闭,重新创建,这意味着该活动将不会在后面的可见弹出(虽然我没有测试过这一点)

As far as I can see, the only disadvantage of this might be that when a popup is on screen, the activity is deleted and has to be recreated when the popup is closed, meaning the activity won't be visible behind the popup (although I have not tested this)

这可能需要更长的时间来重新开始活动,但因为系统可能反正删除的活动没有任何通知,你必须拯救整个国家的反正。

It may take a bit longer to restart the activity, but since the system could have deleted the activity anyway without any notice, you have to save the whole state anyway.

有什么想法?

更新: 我想我在想什么的是,其中一个头版活动调用一个游戏活动。在FrontPage活动将调用游戏活动时,玩家点击播放

Update: I suppose what I was thinking of was where a 'front page' activity calls a game activity. The frontpage activity would call the game activity when the player clicks 'Play'

游戏活动将设立在的onCreate意见和听众等,并在onResume它会加载包含游戏状态的文件,或开始新游戏,如果没有文件存在。

The game activity would set up its views and listeners etc. in onCreate, and in onResume it would load a file containing the game state, or start a new game if no file existed.

的onPause的游戏,它写的游戏状态的文件,然后不管发生什么游戏活动(什么都没有,或者被停止/销毁,或其他)的onResume方法总是从装载所有的数据再次插入文件。

onPause of the game, it writes the game state to the file, then whatever happens to the game activity (nothing, or gets stopped/destroyed, or whatever) the onResume method would always load all the data back in again from the file.

这是那种我在想什么,如果让任何意义吗?

That's sort of what I was thinking, if that makes any sense?

UPDATE2: 我设计我已经记录在下面回答一个简单的解决方案,如果任何人的兴趣!

Update2: I've devised a simple solution which I've documented in an answer below, if anyone's interested!

它不支持Android活动的生命周期'暂停'和'停止'状态。一旦不再显示它杀死自己,并具有手动重新启动,但它并继续从你离开的地方!

It doesn't support the Android Activity Lifecycle 'Paused' and 'Stopped' states. Once it is no longer displayed it kills itself and has to be restarted manually, but it does carry on from where you left off!

推荐答案

您在找这个?

要进一步回答你的问题,是的,你可以清楚地从上面的图中的简单(的方法调用,即编号最小的)生命周期确实是的onCreate()看; ONSTART(); onResume();在onPause();

To further answer your question, yes, as you can plainly see from the above diagram the "simplest" (i.e. smallest number of method calls) lifecycle is indeed onCreate(); onStart(); onResume(); onPause();.

您也应该知道关于的onSaveInstanceState() onRetainNonConfigurationInstance()。这是不会生命周期方法。

You should also know about onSaveInstanceState() and onRetainNonConfigurationInstance(). These are NOT lifecycle methods.

所有这些方法都非常有据可查的。请仔细阅读本文件。

All these methods are very well documented. Please read this documentation thoroughly.

要进一步明确的事情,这里有一对夫妇的真实生活情景:

To clarify things further, here are a couple of real-life scenarios:

在活动正在运行,其他活动都在它的上面,的onPause 被调用。系统内存不足,调用的onSaveInstanceState ,杀死活动。用户pressed回来几次,活动已被重新初始化。(pferably使用保存在的onSaveInstanceState 数据$ P $) 在活动正在运行时,用户presses回来。此时 onPause->的onDestroy 被调用,而不调用的onSaveInstanceState Activity is running, other activities come on top of it, onPause is called. System runs out of memory, calls onSaveInstanceState, kills activity. User pressed back a few times, activity has to be re-instantiated (preferably using the data saved in onSaveInstanceState). Activity is running, user presses back. At this point onPause->onDestroy are called, without calling onSaveInstanceState.

您应该明白与的onPause 的onSaveInstanceState 的本质区别。前者是总是调用,而后者当活动的实例的也许的重新初始化,未来只调用。根据这一思路,你的用户会期望两件事情:

You should understand the essential difference between onPause and onSaveInstanceState. The former is always called, while the latter is only called when the activity instance might be re-instantiated in the future. Following this train of thought, your users will expect two things:

当他们从你的活动导航离开,后来回来了,他们希望在他们离开时完全一样的情况下(这将使用实现的onSaveInstanceState ) 。他们没有想到的是,如果他们的退出您的活动。但是: 在他们期望这些数据的双方已签署将被持久化(这将在的onPause 来完成)。举例来说,如果他们开始撰写邮件时,他们会希望看到它作为一个草案,下一次他们回来,即使他们退出了活动。 When they navigate away from your Activity and later come back to it, they want it in the exact same instance that they left it (this would be achieved using onSaveInstanceState). They don't expect that if they exit your activity. However: They will expect that data they have entered will be persisted (which will be done in onPause). For example, if they started composing a message, they'll expect to see it as a draft the next time they come back, even if they exited the activity.

您应该明白这些方法是如何的应该的,以便用来获取你的用户的期望。你如何的实际上的使用他们是由您,您的需求,您的应用程序的本质。

You should understand how these methods are supposed to be used in order to get what your users expect. How you actually use them is up to you, your needs, and your app's nature.