Android活动状态和生存期

活动状态

1.运行状态(顶层的活动)

2.暂停状态(非顶层的,可见的活动)

3.停止状态(非顶层的,不可见的活动)

4.销毁状态(保证手机的内存充足)

活动的生存期

完整的生存期

onCreate活动第一次被创建的时候调用。

onDestroy这个方法在活动被销毁前调用。

可见的生存期

onStart活动不可见变为可见的时候调用。

onStop这个方法在活动完全不可见的时候调用。

前台生存期

onResume活动准备好和用户进行交互的时候调用,活动位于栈顶。

onPause系统准备去启动或者恢复另一个活动的时候调用。

onRestart这个方法在活动由停止变为运行前调用。

看例子:
a.主活动

package activitytest.example.com.activitylifecycletest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate");
        setContentView(R.layout.activity_main);

        // 获取丢失的临时数据
        if (savedInstanceState != null) {
            String tempData = savedInstanceState.getString("data_key");
            Log.d(TAG,tempData);
        }

        // 给两个按钮赋予事件
        Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity);
        Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity);


        startNormalActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,NormalActivity.class);
                startActivity(intent);
            }
        });

        startDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,DialogActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected  void onStart() {
        super.onStart();
        Log.d(TAG,"onStart");
    }

    @Override
    protected  void onResume() {
        super.onResume();
        Log.d(TAG,"onResume");
    }

    @Override
    protected  void onPause() {
        super.onPause();
        Log.d(TAG,"onPause");
    }

    @Override
    protected  void onStop() {
        super.onStop();
        Log.d(TAG,"onStop");
    }

    @Override
    protected  void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }

    @Override
    protected  void onRestart() {
        super.onRestart();
        Log.d(TAG,"onRestart");
    }

    // 确保页面关闭前数据不丢失

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        String tempData = "Something you just typed";
        outState.putString("data_key",tempData);
    }
}

b.正常活动

package activitytest.example.com.activitylifecycletest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class NormalActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.normal_layout);
    }
}

c.弹出层活动

package activitytest.example.com.activitylifecycletest;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class DialogActivity extends Activity { // dialog改为活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
    }
}

d.注册信息

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="activitytest.example.com.activitylifecycletest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NormalActivity" />
        <activity android:name=".DialogActivity"
            android:theme="@android:style/Theme.Dialog">

        </activity>
    </application>

</manifest>

打开主页面
09-01 11:01:02.041 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onCreate
09-01 11:01:02.112 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onStart
09-01 11:01:02.115 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onResume

打开弹出层
09-01 11:01:42.492 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onPause

回到主页面
09-01 11:01:50.875 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onResume

打开正常页面
09-01 11:02:04.768 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onPause
09-01 11:02:05.343 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onStop

回到主页面
09-01 11:02:14.484 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onRestart
09-01 11:02:14.484 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onStart
09-01 11:02:14.485 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onResume

销毁主页面
09-01 11:02:27.208 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onPause
09-01 11:02:27.993 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onStop
09-01 11:02:27.993 14705-14705/activitytest.example.com.activitylifecycletest D/MainActivity: onDestroy

测试发现还挺有意思。

posted @ 2017-09-01 18:02  TBHacker  阅读(437)  评论(0编辑  收藏  举报