13_Android的生命周期


  1. Activity的生命周期图

2 Android生命周期中涉及到的几个过程

1.启动Activity:系统会先调用onCreate方法。然后调用onStart方法。最后调用onResume,Activity进入执行状态。

2. 当前Activity被其它Activity覆盖其上或被锁屏,系统会调用onPause方法。暂停当前Activity的执行。

3.当前Activity由被覆盖状态回到前台或解锁锁屏:系统会调用onResume方法,再次进入执行状态。

4.当前Activity转到的Activity界面或按Home键回到主屏,自身退居后台:系统会先调用onPause方法,然后调用onStop方法。

进入停滞状态。

5.用户后退回到此Activity:系统会先后调用onRestart方法。然后调用onStart方法,最后调用onResume方法,再次进入执行状态。

6.当前Activity处于被覆盖状态或者后台不可见状态,即第2步和第4步,系统内存不足。杀死当前Activity。而后用户退回当前Activity,再次调用onCreate方法、onStart方法、onResume方法,进入执行状态。

7.当用户退出当前Activity:系统先调用onPause方法,然后调用onStop方法,最后调用onDestory方法。结束当前Activity.

3.编写例如以下程序进行測试

4 Android清单文件例如以下:

<?

xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.lifecycle"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima.lifecycle.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity

            android:theme="@android:style/Theme.Translucent"

            android:name="com.itheima.lifecycle.SecondActivity">

        </activity>

    </application>

 

</manifest>

5 布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

   

    <Button

        android:onClick="click"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true"

        android:text="开启第二个界面"/>

   

</RelativeLayout>

布局文件:activity_second.xml

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".SecondActivity" >

   

   <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="我是第二个界面"/>

 

</RelativeLayout>

6 MainActivity的代码例如以下:

package com.itheima.lifecycle;

 

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.View;

 

public class MainActivity extends ActionBarActivity {

 

                      /**

                       * 被创建的时候调用的方法

                       */

                      @Override

                      protected void onCreate(Bundle savedInstanceState) {

                            super.onCreate(savedInstanceState);

                            setContentView(R.layout.activity_main);

                     

                            System.out.println("oncreate");

                      }

 

                      /**

                       * 被销毁的时候调用的方法

                       */

                      @Override

                      protected void onDestroy() {

                            System.out.println("onDestroy");

                            super.onDestroy();

                      }

                     

                      /**

                       * activity界面可见的时候调用的方法

                       * Activity创建或者从后台又一次回到前台时候被调用

                       */

                      @Override

                      protected void onStart() {

                            System.out.println("onStart");

                            super.onStart();

                      }

                     

                      /**

                       * Activity从后台又一次回到前台时被调用

                       */

                      protected void onRestart() {

                            System.out.println("onstart");

                            super.onRestart();

                      }

                     

                      /**

                       * activity界面用户不可见的时候调用的方法

                       */

                      @Override

                      protected void onStop() {

                            System.out.println("onStop");

                            super.onStop();

                      }

                     

                      /**

                       * 界面開始获取到焦点相应的方法。

                       * (界面button能够被点击,文本框能够输入内容)

                       *

                       * Activity创建或者从被覆盖、后台又一次回到前台时被调用

                       */

                      @Override

                      protected void onResume() {

                            System.out.println("onresume");

                            super.onResume();

                      }

                     

                      /**

                       * 界面失去焦点相应的方法(暂停)

                       * (button不可被点击,文本框不可,可是界面输入内容,用户仍然能看见)

                       */

                      @Override

                      protected void onPause() {

                            System.out.println("onpause");

                            super.onPause();

                            //可能在执行完onPauseonStop后,系统资源紧张将Activity杀死,所以有

                            //必要在此保存持久数据

                      }

                     

                      /**

                       * 窗体获得或失去焦点是被调用,在onResume之后或onPause之后

                       */

                      public void onWindowFocusChanged(boolean hasFocus) {

                            System.out.println("onWindowFocusChanged");

                            super.onWindowFocusChanged(hasFocus);

                      }

                     

                      /**

                       * Activity被系统杀死是被调用。

                       * 比如:屏幕方向改变时。Activity被销毁在重建;当前Activity处于后台,系统资源

                       * 紧张将其杀死。

                       * 另外。当跳转到其它Activity或者按Home键回到主屏是该方法也被调用,系统为了保存

                       * 当前View组件。在onPause之后前被调用。

                       */

                      protected void onSaveInstanceState(Bundle outState) {

                            System.out.println("onSaveInstanceState");

                            super.onSaveInstanceState(outState);

                      }

                     

                      /**

                       * Activity被系统杀死后再重建时被调用

                       * 比如:屏幕方向改变时。Activity被销毁在重建,当前Activity处于后台,系统

                       * 资源紧张将其杀死,用户又启动该Activity,这两种情况下onRestoreInstanceState

                       * 都被调用,在onStart之后。

                       */

                      protected void onRestoreInstanceState(Bundle savedInstanceState) {

                            System.out.println("onRestoreInstanceState");

                            super.onRestoreInstanceState(savedInstanceState);

                      }

                     

                      public void click(View view) {

                            Intent intent = new Intent(this,SecondActivity.class);

                            startActivity(intent);

                      }

}

7 SecondActivity的内容例如以下:

package com.itheima.lifecycle;

 

import android.app.Activity;

import android.os.Bundle;

 

public class SecondActivity extends Activity {

 

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);                                                               

                   setContentView(R.layout.activity_second);

         }

}

 

posted on 2017-05-17 10:02  ljbguanli  阅读(137)  评论(0编辑  收藏  举报