01 package com.devdiv.test.activitytest; 02 03 04 import android.app.Activity; 05 import android.content.Intent; 06 import android.content.res.Configuration; 07 import android.os.Bundle; 08 import android.util.Log; 09 import android.view.View; 10 11 public class ActivityTestActivity extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 18 19 if (savedInstanceState == null) { 20 Log.d("test", "ActivityTestActivity - onCreate()"); 21 } else { 22 Log.d("test","ActivityTestActivity - onCreate() with a supplied savedInstanceState"); 23 } 24 } 25 26 @Override 27 public void onConfigurationChanged(Configuration newConfig) { 28 // TODO Auto-generated method stub 29 super.onConfigurationChanged(newConfig); 30 Log.d("test", "ActivityTestActivity - onConfigurationChanged()"); 31 } 32 33 @Override 34 protected void onDestroy() { 35 // TODO Auto-generated method stub 36 super.onDestroy(); 37 Log.d("test", "ActivityTestActivity - onDestroy()"); 38 } 39 40 @Override 41 protected void onPause() { 42 // TODO Auto-generated method stub 43 super.onPause(); 44 Log.d("test", "ActivityTestActivity - onPause()"); 45 } 46 47 @Override 48 protected void onRestart() { 49 // TODO Auto-generated method stub 50 super.onRestart(); 51 Log.d("test", "ActivityTestActivity - onRestart()"); 52 } 53 54 @Override 55 protected void onRestoreInstanceState(Bundle savedInstanceState) { 56 // TODO Auto-generated method stub 57 super.onRestoreInstanceState(savedInstanceState); 58 Log.d("test", "ActivityTestActivity - onRestoreInstanceState()"); 59 } 60 61 @Override 62 protected void onResume() { 63 // TODO Auto-generated method stub 64 super.onResume(); 65 Log.d("test", "ActivityTestActivity - onResume()"); 66 } 67 68 @Override 69 protected void onSaveInstanceState(Bundle outState) { 70 // TODO Auto-generated method stub 71 super.onSaveInstanceState(outState); 72 Log.d("test", "ActivityTestActivity - onSaveInstanceState()"); 73 } 74 75 @Override 76 protected void onStart() { 77 // TODO Auto-generated method stub 78 super.onStart(); 79 Log.d("test", "ActivityTestActivity - onStart()"); 80 } 81 82 @Override 83 protected void onStop() { 84 // TODO Auto-generated method stub 85 super.onStop(); 86 Log.d("test", "ActivityTestActivity - onStop()"); 87 } 88 89 public void onButtonClick(View v) { 90 Intent mIntent = new Intent(this, SecondActivity.class); 91 startActivity(mIntent); 92 } 93 94 95 }
SecondActivity.class的代码:
01 package com.devdiv.test.activitytest; 02 03 04 import android.app.Activity; 05 import android.content.Intent; 06 import android.content.res.Configuration; 07 import android.os.Bundle; 08 import android.util.Log; 09 import android.view.View; 10 11 public class ActivityTestActivity extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 18 19 if (savedInstanceState == null) { 20 Log.d("test", "ActivityTestActivity - onCreate()"); 21 } else { 22 Log.d("test","ActivityTestActivity - onCreate() with a supplied savedInstanceState"); 23 } 24 } 25 26 @Override 27 public void onConfigurationChanged(Configuration newConfig) { 28 // TODO Auto-generated method stub 29 super.onConfigurationChanged(newConfig); 30 Log.d("test", "ActivityTestActivity - onConfigurationChanged()"); 31 } 32 33 @Override 34 protected void onDestroy() { 35 // TODO Auto-generated method stub 36 super.onDestroy(); 37 Log.d("test", "ActivityTestActivity - onDestroy()"); 38 } 39 40 @Override 41 protected void onPause() { 42 // TODO Auto-generated method stub 43 super.onPause(); 44 Log.d("test", "ActivityTestActivity - onPause()"); 45 } 46 47 @Override 48 protected void onRestart() { 49 // TODO Auto-generated method stub 50 super.onRestart(); 51 Log.d("test", "ActivityTestActivity - onRestart()"); 52 } 53 54 @Override 55 protected void onRestoreInstanceState(Bundle savedInstanceState) { 56 // TODO Auto-generated method stub 57 super.onRestoreInstanceState(savedInstanceState); 58 Log.d("test", "ActivityTestActivity - onRestoreInstanceState()"); 59 } 60 61 @Override 62 protected void onResume() { 63 // TODO Auto-generated method stub 64 super.onResume(); 65 Log.d("test", "ActivityTestActivity - onResume()"); 66 } 67 68 @Override 69 protected void onSaveInstanceState(Bundle outState) { 70 // TODO Auto-generated method stub 71 super.onSaveInstanceState(outState); 72 Log.d("test", "ActivityTestActivity - onSaveInstanceState()"); 73 } 74 75 @Override 76 protected void onStart() { 77 // TODO Auto-generated method stub 78 super.onStart(); 79 Log.d("test", "ActivityTestActivity - onStart()"); 80 } 81 82 @Override 83 protected void onStop() { 84 // TODO Auto-generated method stub 85 super.onStop(); 86 Log.d("test", "ActivityTestActivity - onStop()"); 87 } 88 89 public void onButtonClick(View v) { 90 Intent mIntent = new Intent(this, SecondActivity.class); 91 startActivity(mIntent); 92 } 93 94 95 } SecondActivity.class的代码: 01 package com.devdiv.test.activitytest; 02 03 import android.app.Activity; 04 import android.content.res.Configuration; 05 import android.os.Bundle; 06 import android.util.Log; 07 08 public class SecondActivity extends Activity { 09 10 /** Called when the activity is first created. */ 11 @Override 12 public void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.second); 15 16 17 if (savedInstanceState == null) { 18 Log.d("test", "SecondActivity - onCreate()"); 19 } else { 20 Log.d("test","SecondActivity - onCreate() with a supplied savedInstanceState"); 21 } 22 } 23 24 @Override 25 public void onConfigurationChanged(Configuration newConfig) { 26 // TODO Auto-generated method stub 27 super.onConfigurationChanged(newConfig); 28 Log.d("test", "SecondActivity - onConfigurationChanged()"); 29 } 30 31 @Override 32 protected void onDestroy() { 33 // TODO Auto-generated method stub 34 super.onDestroy(); 35 Log.d("test", "SecondActivity - onDestroy()"); 36 } 37 38 @Override 39 protected void onPause() { 40 // TODO Auto-generated method stub 41 super.onPause(); 42 Log.d("test", "SecondActivity - onPause()"); 43 } 44 45 @Override 46 protected void onRestart() { 47 // TODO Auto-generated method stub 48 super.onRestart(); 49 Log.d("test", "SecondActivity - onRestart()"); 50 } 51 52 @Override 53 protected void onRestoreInstanceState(Bundle savedInstanceState) { 54 // TODO Auto-generated method stub 55 super.onRestoreInstanceState(savedInstanceState); 56 Log.d("test", "SecondActivity - onRestoreInstanceState()"); 57 } 58 59 @Override 60 protected void onResume() { 61 // TODO Auto-generated method stub 62 super.onResume(); 63 Log.d("test", "SecondActivity - onResume()"); 64 } 65 66 @Override 67 protected void onSaveInstanceState(Bundle outState) { 68 // TODO Auto-generated method stub 69 super.onSaveInstanceState(outState); 70 Log.d("test", "SecondActivity - onSaveInstanceState()"); 71 } 72 73 @Override 74 protected void onStart() { 75 // TODO Auto-generated method stub 76 super.onStart(); 77 Log.d("test", "SecondActivity - onStart()"); 78 } 79 80 @Override 81 protected void onStop() { 82 // TODO Auto-generated method stub 83 super.onStop(); 84 Log.d("test", "SecondActivity - onStop()"); 85 } 86 87 88 }
图1 模拟器LogCat日志信息
某些情况下,并不希望在旋转屏幕时Activity被重新加载,这时可以通过修改Android应用工程的Androidmanifest.xml文件中Activity节点的android:configChanges属性,通常需要把这个属性设置为android:configChanges="orientation|navigation|keyboardHidden",这表示当屏幕旋转,页面转换以及键盘隐藏时都不要重新加载Activity,而是调用onConfigurationChanged方法。在上面的代码中可以看到SecondActivity覆盖了Activity的onConfigurationChanged方法,并设置了activity的android:configChanges属性。
接下来点击按钮来启动SecondActivity,然后旋转屏幕再看一下输出的日志信息,从图2-11中可以清楚的看到,当切换屏幕显示模式时,SecondActivity并没有被重新加载,只是调用了它的onConfigurationChanged()方法,如果并不希望在旋转屏幕时Activity被重新加载,那么需要实现Activity的onConfigurationChanged方法进行添加相应的的处理逻辑。
图2 模拟器LogCat日志信息