Android Activity 生命周期

代码:

Android Activity 生命周期
package com.example.testandroid;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

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

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

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

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

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

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }
    
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i(TAG, "onConfigurationChanged");
    }
}
Android Activity 生命周期
Android Activity 生命周期
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testandroid.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>
    </application>

</manifest>
Android Activity 生命周期

 

1.启动:

Android Activity 生命周期
03-22 09:41:25.305: I/TAG(25275): onCreate
03-22 09:41:25.305: I/TAG(25275): onStart
03-22 09:41:25.305: I/TAG(25275): onResume
Android Activity 生命周期

2.按home键

1
2
03-22 09:42:06.466: I/TAG(25275): onPause
03-22 09:42:07.653: I/TAG(25275): onStop

3.重新启动activity

Android Activity 生命周期
03-22 09:42:54.027: I/TAG(25275): onRestart
03-22 09:42:54.028: I/TAG(25275): onStart
03-22 09:42:54.028: I/TAG(25275): onResume
Android Activity 生命周期

4.按返回键

Android Activity 生命周期
03-22 09:43:28.857: I/TAG(25275): onPause
03-22 09:43:29.499: I/TAG(25275): onStop
03-22 09:43:29.500: I/TAG(25275): onDestroy
Android Activity 生命周期

4.竖屏切换横屏

Android Activity 生命周期
03-22 09:46:48.576: I/TAG(26113): onPause
03-22 09:46:48.577: I/TAG(26113): onSaveInstanceState
03-22 09:46:48.577: I/TAG(26113): onStop
03-22 09:46:48.577: I/TAG(26113): onDestroy
03-22 09:46:48.715: I/TAG(26113): onCreate
03-22 09:46:48.716: I/TAG(26113): onStart
03-22 09:46:48.720: I/TAG(26113): onRestoreInstanceState
03-22 09:46:48.720: I/TAG(26113): onResume
Android Activity 生命周期

5.横屏切换竖屏

1
2
3
4
5
6
7
8
03-22 09:47:29.406: I/TAG(26113): onPause
03-22 09:47:29.422: I/TAG(26113): onSaveInstanceState
03-22 09:47:29.428: I/TAG(26113): onStop
03-22 09:47:29.428: I/TAG(26113): onDestroy
03-22 09:47:29.521: I/TAG(26113): onCreate
03-22 09:47:29.521: I/TAG(26113): onStart
03-22 09:47:29.523: I/TAG(26113): onRestoreInstanceState
03-22 09:47:29.523: I/TAG(26113): onResume

  

切换都一次 不知道为什么有些资料说切竖屏两次打印

Android Activity 生命周期,布布扣,bubuko.com

Android Activity 生命周期

上一篇:nios 使用count binary 例程 只是led不闪


下一篇:Android中的Activity四种启动模式(launchMode)