Android - activity 的生命周期常用方法概述

2017-08-18  本文已影响0人  DJN_

Android-activity的生命周期常用方法概述

activity生命周期

  1. activity大致会经过如下四种状态
  1. activity生命周期的常用方法
1.onCreate:

在activity的生命周期中这个方法是创建activity后第一个被系统调用的方
法。该方法只会调用一次。
Bundle savedInstanceState:Bundle类型的数据类似Map,以key-value
形式存储数据。

If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null.

如果活动在先前被关闭之后被重新初始化,那么该Bundle包含最近在
onSaveInstanceState(Bundle)中提供的数据。 注意:否则为null。

savedInstanceState参数的来源是activity生命周期的onSaveInstanceState方法
那<font color="red">活动在先前被关闭之后被重新初始化</font>指的是那些情况呢?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
API 21 加入的三个重载方法

API 21 为activity新增了一个属性android:persistableMode,将该属性设置
persistAcrossRebootsactivity就具有持久化的能力,另外需要配合一个新
的BundlePersistableBundle来保存数据。

public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)

public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState)

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
2.onStart

Called after onCreate — or after onRestart when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

    @Override
    protected void onStart() {
        super.onStart();
    }
3.onResume

Called after onRestoreInstanceState, onRestart, or onPause, for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged to know for certain that your activity is visible to the user (for example, to resume a game).Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

    @Override
    protected void onResume() {
        super.onResume();
    }
4.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.This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)After receiving this call you will usually receive a following call to onStop (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume without going through the stopped state.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

    @Override
    protected void onPause() {
        super.onPause();
    }
5.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.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

    @Override
    protected void onStop() {
        super.onStop();
    }
6.onDestroy

Perform any final cleanup before an 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.Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause or onSaveInstanceState, not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

<center>还有一张来自stromzhang公众号的图:</center>

这里写图片描述

参考:Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?


上一篇 下一篇

猜你喜欢

热点阅读