Android的活动的生命周期 - 这都是什么方法? [英] Android activity life cycle - what are all these methods for?

查看:166
本文介绍了Android的活动的生命周期 - 这都是什么方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个Android活动的生命周期?为什么有这么多相似的测深方法(的onCreate() ONSTART() onResume( ))称为初始化过程中,和许多其他人(的onPause()的onStop()的onDestroy())呼吁在结束了吗?

What is the life cycle of an Android activity? Why are so many similar sounding methods (onCreate(), onStart(), onResume()) called during initialization, and so many others (onPause(), onStop(), onDestroy()) called at the end?

当是这些方法调用,应如何正确使用他们?

When are these methods called, and how should they be used properly?

推荐答案

请参阅它的活动生命周期 的(在Android开发者)。

See it in Activity Lifecycle (at Android Developers).

<一个href="http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29">onCreate():

onCreate():

第一次创建活动时调用。这是你应该做的   所有的正常静态设置:创建视图,绑定数据到列表,   等等。这种方法还提供了一个Bundle包含   活动的previously冻结状态,如果有的话。始终遵循   通过ONSTART()。

Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

<一个href="http://developer.android.com/reference/android/app/Activity.html#onRestart%28%29">onRestart():

onRestart():

调用后您的活动已经停止,之前它被启动   再次。总是跟着ONSTART()

Called after your activity has been stopped, prior to it being started again. Always followed by onStart()

<一个href="http://developer.android.com/reference/android/app/Activity.html#onStart%28%29">onStart():

onStart():

当的活性正成为对用户可见调用。其次是   onResume()如果活动涉及到前台,或的onStop(),如果它   被隐藏。

Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

<一个href="http://developer.android.com/reference/android/app/Activity.html#onResume%28%29">onResume():

onResume():

调用时,活动将开始与用户交互。在这   点你活动是在活动堆栈的顶部,与用户   输入要它。总是跟随在onPause()。

Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause().

的onPause()

onPause ():

古称活动周期的一部分,当活动是怎么回事   到背景中,   但还没有(还)被杀害。在对口onResume()。   当活动B为活性的前推出,这个回调将在A.调用   B就不会被创建,直到A的的onPause()返回,所以一定不要   做任何事情冗长这里。

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

<一个href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29">onStop():

onStop():

当你不再对用户可见调用。你将明年   接受或onRestart()的onDestroy(),或没有,这取决于   以后用户的活动。

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

请注意,此方法可能永远不会被调用,在内存不足的情况​​下,   其中,系统没有足够的内存,让您的活动的   它的onPause()方法被调用后运行的进程。

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

<一个href="http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29">onDestroy():

onDestroy():

您的活动前收到的最后通话将被破坏。本   适用于以下情况,因为该活动结束(有人称为   完成()就可以了,或因为系统暂时销毁该   活动的实例以节省空间。您可以区分   这两个方案与isFinishing()方法

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

在活动的第一次加载事件被称为如下:

When the Activity first time loads the events are called as below:

onCreate()
onStart()
onResume()

点击手机按钮活动转到背景和下面的事件被称为:

When you click on Phone button the Activity goes to the background and the below events are called:

onPause()
onStop()

退出电话拨号器,然后下面的事件将被称为:

Exit the phone dialer and the below events will be called:

onRestart()
onStart()
onResume()

当您点击后退按钮或试图完成()事件被称为以下活动:

When you click the back button OR try to finish() the activity the events are called as below:

onPause()
onStop()
onDestroy()


活动状态


Activity States

Android操作系统使用优先级队列,以协助管理的设备上运行的活动。基于状态的特定机器人活动是,它会被操作系统中分配一定的优先权。该优先系统可以帮助机器人识别不再使用的活动,让操作系统回收内存和资源。下图显示了状态的活动可以通过,在其一生中:

The Android OS uses a priority queue to assist in managing activities running on the device. Based on the state a particular Android activity is in, it will be assigned a certain priority within the OS. This priority system helps Android identify activities that are no longer in use, allowing the OS to reclaim memory and resources. The following diagram illustrates the states an activity can go through, during its lifetime:

这些状态可如下分成三个主要的组:

These states can be broken into three main groups as follows:

活动或运行 - 活动被认为是有效或运行,如果他们是在前台,也被称为活性堆栈的顶部。这被认为是在Android活动堆栈中的最高优先级的活动,因此将只有在极端情况下,比如如果活动尝试使用更多的内存比操作系统被杀死,请在设备上,因为这可能会导致用户界面不响应。

Active or Running - Activities are considered active or running if they are in the foreground, also known as the top of the activity stack. This is considered the highest priority activity in the Android Activity stack, and as such will only be killed by the OS in extreme situations, such as if the activity tries to use more memory than is available on the device as this could cause the UI to become unresponsive.

暂停 - 当设备进入睡眠状态,或活动仍是可见的,但一部分被新的,非全尺寸或透明的活动,该活动被视为暂停。暂停活动还活着,也就是说,他们认为所有的状态和成员信息,并且仍然贴在窗口管理器。这被认为是在Android活动堆栈中的次高优先级的活动,正因为如此,只会由OS杀死,如果杀死本次活动将满足需要保持主动/跑步活动稳定,反应灵敏的资源需求。

Paused - When the device goes to sleep, or an activity is still visible but partially hidden by a new, non-full-sized or transparent activity, the activity is considered paused. Paused activities are still alive, that is, they maintain all state and member information, and remain attached to the window manager. This is considered to be the second highest priority activity in the Android Activity stack and, as such, will only be killed by the OS if killing this activity will satisfy the resource requirements needed to keep the Active/Running Activity stable and responsive.

停止 - 活动是由另一活动完全遮蔽被视为停止或背景。停止活动还是尽量保持其状态和成员信息,只要有可能,而是停的活动都被认为是三种状态的优先级最低,正因为如此,该操作系统将杀死活动在此状态下首先要满足资源需求高优先级的活动。

Stopped - Activities that are completely obscured by another activity are considered stopped or in the background. Stopped activities still try to retain their state and member information for as long as possible, but stopped activities are considered to be the lowest priority of the three states and, as such, the OS will kill activities in this state first to satisfy the resource requirements of higher priority activities.

样的活动,了解生命周期的*

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
    String tag = "LifeCycleEvents";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Log.d(tag, "In the onCreate() event");
    }
    public void onStart()
    {
       super.onStart();
       Log.d(tag, "In the onStart() event");
    }
    public void onRestart()
    {
       super.onRestart();
       Log.d(tag, "In the onRestart() event");
    }
    public void onResume()
    {
       super.onResume();
       Log.d(tag, "In the onResume() event");
    }
    public void onPause()
    {
       super.onPause();
       Log.d(tag, "In the onPause() event");
    }
    public void onStop()
    {
       super.onStop();
       Log.d(tag, "In the onStop() event");
    }
    public void onDestroy()
    {
       super.onDestroy();
       Log.d(tag, "In the onDestroy() event");
    }
}

这篇关于Android的活动的生命周期 - 这都是什么方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆