Android Activity 生命周期 - 所有这些方法的用途是什么? [英] Android activity life cycle - what are all these methods for?

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

问题描述

Android Activity 的生命周期是什么?为什么在初始化过程中调用了这么多类似的听起来方法(onCreate()onStart()onResume()),还有这么多其他(onPause(), onStop(), onDestroy()) 最后调用?

这些方法什么时候调用,应该如何正确使用?

解决方案

查看:

<块引用>

首次创建活动时调用.这是你应该做的地方所有正常的静态设置:创建视图,将数据绑定到列表,等.此方法还为您提供了一个包含活动的先前冻结状态,如果有的话.一直关注通过 onStart().

onRestart():

<块引用>

在您的活动停止后、活动开始前调用再次.总是跟着 onStart()

onStart():

<块引用>

当活动对用户可见时调用.其次是onResume() 如果 Activity 出现在前台.

onResume():

<块引用>

在 Activity 开始与用户交互时调用.在这指向您的活动位于活动堆栈的顶部,用户输入去它.总是跟在 onPause() 之后.

onPause():

<块引用>

在活动进行时作为活动生命周期的一部分被调用进入背景,但还没有(还)被杀死.对应于 onResume().当活动 B 在活动 A 之前启动时,将在 A 上调用此回调.B 在 A 的 onPause() 返回之前不会被创建,所以一定不要在这里做任何冗长的事情.

onStop():

<块引用>

当您不再对用户可见时调用.你接下来接收 onRestart()、onDestroy() 或不接收,具体取决于以后的用户活动.请注意,在内存不足的情况下,可能永远不会调用此方法系统没有足够的内存来保存您的活动进程在其 onPause() 方法被调用后运行.

onDestroy():

<块引用>

您在 Activity 被销毁之前收到的最后一个调用.这个可能因为活动正在完成而发生(有人打电话给finish() 就可以了,或者因为系统暂时销毁了这个活动实例以节省空间.您可以使用 isFinishing() 方法区分>这两种情况.

当活动第一次加载时,事件被调用如下:

onCreate()开始()恢复()

当您点击电话按钮时,活动进入后台并调用以下事件:

onPause()停止()

退出电话拨号器,将调用以下事件:

onRestart()开始()恢复()

当您点击返回按钮或尝试finish()活动时,事件调用如下:

onPause()停止()销毁()

<小时>

活动状态

Android 操作系统使用优先级队列来协助管理设备上运行的活动.根据特定 Android Activity 所处的状态,它会在操作系统中被分配特定的优先级.此优先级系统可帮助 Android 识别不再使用的活动,从而允许操作系统回收内存和资源.下图说明了 Activity 在其生命周期中可以经历的状态:

这些状态可以分为以下三个主要组:

活动或正在运行 - 如果活动位于前台(也称为活动堆栈的顶部),则它们被视为活动或正在运行.这被认为是 Android Activity 堆栈中优先级最高的 Activity,因此只会在极端情况下被操作系统杀死,例如,如果 Activity 尝试使用比设备上可用的内存更多的内存,因为这可能导致 UI变得没有反应.

暂停 - 当设备进入睡眠状态,或者某个活动仍然可见但被新的、非全尺寸或透明的活动部分隐藏时,该活动被视为已暂停.暂停的活动仍然活着,即它们维护所有状态和成员信息,并保持附加到窗口管理器.这被认为是 Android Activity 堆栈中的第二高优先级 Activity,因此,只有在终止此 Activity 将满足保持活动/正在运行的 Activity 稳定和响应所需的资源需求时,才会被操作系统终止.

已停止 - 完全被另一个活动遮挡的活动被视为已停止或在后台.已停止的活动仍会尽可能长时间地保留其状态和成员信息,但已停止的活动被认为是三种状态中优先级最低的,因此操作系统将首先终止处于此状态的活动以满足资源需求优先级更高的活动.

*了解生命周期的示例活动**

import android.app.Activity;导入 android.os.Bundle;导入 android.util.Log;公共类 MainActivity 扩展 Activity {String tag = "LifeCycleEvents";/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.d(tag, "在 onCreate() 事件中");}公共无效 onStart(){super.onStart();Log.d(tag, "在 onStart() 事件中");}公共无效 onRestart(){super.onRestart();Log.d(tag, "在 onRestart() 事件中");}公共无效 onResume(){super.onResume();Log.d(tag, "在 onResume() 事件中");}公共无效 onPause(){super.onPause();Log.d(tag, "在 onPause() 事件中");}公共无效 onStop(){super.onStop();Log.d(tag, "在 onStop() 事件中");}公共无效 onDestroy(){super.onDestroy();Log.d(tag, "在 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?

解决方案

See it in Activity Lifecycle (at Android Developers).

onCreate():

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().

onRestart():

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

onStart():

Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground.

onResume():

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 ():

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.

onStop():

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity. 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.

onDestroy():

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

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:

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.

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.

*Sample activity to understand the life cycle**

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 Activity 生命周期 - 所有这些方法的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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