Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 面試之橫豎屏切換的Activity生命周期

Android 面試之橫豎屏切換的Activity生命周期

編輯:關於Android編程

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生命周期:

\

④換回豎屏的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鍵之後的生命周期:

\
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved