Android 橫豎屏切換的Activity生命周期演示-面試常問題

Android 橫豎屏切換的Activity生命周期,很多公司和企業面試會問到橫豎屏切換的Activity生命周期、Handler機制、單例模式

等,不過這幾個是常問的,這篇博客主要說說橫豎屏切換時Activity的生命周期


① 創建一個Activity,分別標註測試語句


package com.example.engineerjspactivitry;
/**
 * Android 橫豎屏切換Activity生命周期
 * @author Engineer-Jsp
 * @date 2014.10.27
 * */
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class EngineerJspActivity extends Activity {
    private static String Tag = "EngineerJspActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		Log.d(Tag, "onCreate()");
		setContentView(R.layout.activity_main);
	}
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(Tag, "onStart()");
	}
    @Override
    protected void onResume() {
    	super.onResume();
    	Log.d(Tag, "onResume()");
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	Log.d(Tag, "onSaveInstanceState()");
    }
    @Override
    protected void onPause() {
    	super.onPause();
    	Log.d(Tag, "onPause()");
    }
    @Override
    protected void onStop() {
    	super.onStop();
    	Log.d(Tag, "onStop()");
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	Log.d(Tag, "onRestoreInstanceState()");
    }
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	Log.d(Tag, "onDestroy()");
    }

}


②運行Activity(沒有切換屏幕的正常情況下啟動Activity),得到如下結果:



③切換成橫屏時的Activity生命周期:vc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz48aW1nIHNyYz0="/uploadfile/Collfiles/20141106/20141106081350160.png" alt="\">

④換回豎屏的Activity生命周期:


⑤給清單文件加上android:configChanges="orientation"屬性,重新啟動Activity,沒有切換屏幕時(正常啟動)的生命周期:


⑥加上android:configChanges="orientation"屬性,切換成橫屏,Activity的生命周期:


⑦加上android:configChanges="orientation"屬性,換回豎屏,Activity的生命周期:


⑧加上android:configChanges="orientation|keyboardHidden"屬性,正常啟動Activity的生命周期(未切屏幕):


⑨加上android:configChanges="orientation|keyboardHidden"屬性,正常啟動Activity的生命周期(切成橫屏):


⑩加上android:configChanges="orientation|keyboardHidden"屬性,正常啟動Activity的生命周期(換回豎屏):


執行Home鍵之後的生命周期:



本人是拿平板測試的,結果如有出入,跟真實結果無關,謝謝~


發佈留言

發佈留言必須填寫的電子郵件地址不會公開。