本文翻译自:Android activity life cycle - what are all these methods for?
What is the life cycle of an Android activity? Android活动的生命周期是什么? Why are so many similar sounding methods ( onCreate()
, onStart()
, onResume()
) called during initialization, and so many others ( onPause()
, onStop()
, onDestroy()
) called at the end? 为什么在初始化期间调用了如此多的类似声音方法( onCreate()
, onStart()
, onResume()
)以及最后调用的其他许多其他声明方法( onPause()
, onStop()
, onDestroy()
)?
When are these methods called, and how should they be used properly? 这些方法何时被调用,它们应该如何正确使用?
#1楼
参考:https://stackoom.com/question/ZjNo/Android活动生命周期-这些方法有哪些
#2楼
ANDROID LIFE-CYCLE ANDROID LIFE-CYCLE
There are seven methods that manage the life cycle of an Android application: 有七种方法可以管理Android应用程序的生命周期:
- onCreate() 的onCreate()
- onStart() 在onStart()
- onResume() 的onResume()
- onRestart() onRestart()
- onPause() 的onPause()
- onStop() 的onStop()
- onDestroy() 的onDestroy()
Answer for what are all these methods for: 回答所有这些方法:
Let us take a simple scenario where knowing in what order these methods are called will help us give a clarity why they are used. 让我们看一个简单的场景,知道调用这些方法的顺序将有助于我们清楚地说明它们的用法。
- Suppose you are using a calculator app. 假设您使用的是计算器应用程序。 Three methods are called in succession to start the app. 连续调用三种方法来启动应用程序。
onCreate()
- - - > onStart()
- - - > onResume()
onCreate()
- - - > onStart()
- - - > onResume()
- When I am using the calculator app, suddenly a call comes the. 当我使用计算器应用程序时,突然有一个电话来了。 The calculator activity goes to the background and another activity say. 计算器活动转到后台,另一个活动说。 Dealing with the call comes to the foreground, and now two methods are called in succession. 处理呼叫到达前台,现在连续调用两个方法。
onPause()
- - - > onStop()
onPause()
- - - > onStop()
- Now say I finish the conversation on the phone, the calculator activity comes to foreground from the background, so three methods are called in succession. 现在说我在电话上完成了对话,计算器活动从后台开始变为前景,因此连续调用了三种方法。