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");
    }
}
<?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>

 

1.启动:

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

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

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

4.按返回键

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

4.竖屏切换横屏

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

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 生命周期,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。