标签:activity生命周期 configchanage orientation android 4.0
路漫漫其修远兮,吾将上下而求索。---屈原《离骚》
可能很多人会感觉Activity很简单,但是经历了半年的android开发,我发现我对Activity的的理解还是比较浅显的,其实Activity并没有我们想象的那么简单今天花了一个下午学习了Activity的生命周期,为以后忘记后迅速回顾,做准备。
一、首先看下官网给出activity的生命周期图
实例代码:
package com.example.activitypractice; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private Button btn_jump; private String saveData= "存放的数据" ; //Activity创建时调用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); btn_jump=(Button) findViewById(R.id. btn_jump); btn_jump.setOnClickListener( new OnClickListener() { public void onClick(View v) { Intent intent= new Intent(MainActivity.this,JumpToActivity.class ); startActivity(intent); } }); Log. i(TAG,"执行了onCreate方法" ); } //Activity创建或者从后台重新回到前台时被调用 @Override protected void onStart() { super.onStart(); Log. i(TAG,"执行了onStart方法" ); } //Activity从后台重新回到前台时被调用 @Override protected void onRestart() { super.onRestart(); Log. i(TAG,"执行了onRestart方法" ); } //Activity创建或者从被覆盖、后台重新回到前台时被调用 @Override protected void onResume() { super.onResume(); Log. i(TAG,"执行了onResume方法" ); } //Activity被覆盖到下面或者锁屏时被调用 @Override protected void onPause() { super.onPause(); Log. i(TAG,"执行了onPause方法" ); } //退出当前Activity或者跳转到新Activity时被调用 @Override protected void onStop() { super.onStop(); Log. i(TAG,"执行了onStop方法" ); } //退出当前Activity时被调用,调用之后Activity就结束了 @Override protected void onDestroy() { super.onDestroy(); Log. i(TAG,"执行了onDestroy方法" ); } //Activity窗口获得或失去焦点时被调用,在onResume之后或onPause之后 /* @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.i(TAG, "onWindowFocusChanged called."); } */ /** * Activity被系统杀死时被调用. * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死. * 另外,当跳转到其他Activity或者按Home键回到主屏时该方法也会被调用,系统是为了保存当前View组件的状态. * 在onPause之前被调用. */ @Override protected void onSaveInstanceState(Bundle outState) { outState.putString( "key", saveData); Log. i(TAG, "onSaveInstanceState called.put saveData: " + saveData ); super.onSaveInstanceState(outState); } /** * Activity被系统杀死后再重建时被调用. * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死,用户又启动该Activity. * 这两种情况下onRestoreInstanceState都会被调用,在onStart之后. */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { String getData = savedInstanceState.getString( "key"); Log. i(TAG, "onRestoreInstacedState called.getData: "+getData); super.onRestoreInstanceState(savedInstanceState); } }
package com.example.activitypractice; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private Button btn_jump; private String saveData= "存放的数据" ; //Activity创建时调用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); btn_jump=(Button) findViewById(R.id. btn_jump); btn_jump.setOnClickListener( new OnClickListener() { public void onClick(View v) { Intent intent= new Intent(MainActivity.this,JumpToActivity.class ); startActivity(intent); } }); Log. i(TAG,"执行了onCreate方法" ); } //Activity创建或者从后台重新回到前台时被调用 @Override protected void onStart() { super.onStart(); Log. i(TAG,"执行了onStart方法" ); } //Activity从后台重新回到前台时被调用 @Override protected void onRestart() { super.onRestart(); Log. i(TAG,"执行了onRestart方法" ); } //Activity创建或者从被覆盖、后台重新回到前台时被调用 @Override protected void onResume() { super.onResume(); Log. i(TAG,"执行了onResume方法" ); } //Activity被覆盖到下面或者锁屏时被调用 @Override protected void onPause() { super.onPause(); Log. i(TAG,"执行了onPause方法" ); } //退出当前Activity或者跳转到新Activity时被调用 @Override protected void onStop() { super.onStop(); Log. i(TAG,"执行了onStop方法" ); } //退出当前Activity时被调用,调用之后Activity就结束了 @Override protected void onDestroy() { super.onDestroy(); Log. i(TAG,"执行了onDestroy方法" ); } /** * Activity被系统杀死时被调用. * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死. * 另外,当跳转到其他Activity或者按Home键回到主屏时该方法也会被调用,系统是为了保存当前View组件的状态. * 在onPause之前被调用. */ @Override protected void onSaveInstanceState(Bundle outState) { outState.putString( "key", saveData); Log. i(TAG, "onSaveInstanceState called.put saveData: " + saveData ); super.onSaveInstanceState(outState); } /** * Activity被系统杀死后再重建时被调用. * 例如:屏幕方向改变时,Activity被销毁再重建;当前Activity处于后台,系统资源紧张将其杀死,用户又启动该Activity. * 这两种情况下onRestoreInstanceState都会被调用,在onStart之后. */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { String getData = savedInstanceState.getString( "key"); Log. i(TAG, "onRestoreInstacedState called.getData: "+getData); super.onRestoreInstanceState(savedInstanceState); } //当指定了android:configChanges="orientation"后,方向改变时onConfigurationChanged被调用,并且activity不再销毁重建 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); switch (newConfig. orientation) { case Configuration. ORIENTATION_PORTRAIT://竖屏 Log. i(TAG,"竖屏" ); setContentView(R.layout. portrait); break; case Configuration. ORIENTATION_LANDSCAPE://横屏 Log. i(TAG,"横屏" ); setContentView(R.layout. landscap); default: break; } } }
标签:activity生命周期 configchanage orientation android 4.0
原文地址:http://blog.csdn.net/dmk877/article/details/45059261