第一个activity
1 package good.go; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.Toast; 9 10 public class TestProcActivity extends Activity { 11 /** Called when the activity is first created. */ 12 13 private Button btn = null; 14 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.main); 19 20 Toast.makeText(getApplicationContext(), "on create", 2000).show(); 21 22 btn = (Button) findViewById(R.id.good); 23 btn.setOnClickListener(new View.OnClickListener() { 24 25 @Override 26 public void onClick(View v) { 27 // TODO Auto-generated method stub 28 Intent i = new Intent(); 29 i.setClass(TestProcActivity.this, num.class); 30 startActivity(i); 31 } 32 }); 33 34 } 35 36 @Override 37 protected void onStart() { 38 // TODO Auto-generated method stub 39 Toast.makeText(getApplicationContext(), "on start", 2000).show(); 40 super.onStart(); 41 } 42 43 @Override 44 protected void onRestart() { 45 // TODO Auto-generated method stub 46 Toast.makeText(getApplicationContext(), "on restart", 2000).show(); 47 super.onRestart(); 48 } 49 50 @Override 51 protected void onResume() { 52 // TODO Auto-generated method stub 53 Toast.makeText(getApplicationContext(), "on resume", 2000).show(); 54 super.onResume(); 55 } 56 57 @Override 58 protected void onPause() { 59 // TODO Auto-generated method stub 60 Toast.makeText(getApplicationContext(), "on pause", 2000).show(); 61 super.onPause(); 62 } 63 64 @Override 65 protected void onStop() { 66 // TODO Auto-generated method stub 67 Toast.makeText(getApplicationContext(), "on stop", 2000).show(); 68 super.onStop(); 69 } 70 71 @Override 72 protected void onDestroy() { 73 // TODO Auto-generated method stub 74 Toast.makeText(getApplicationContext(), "on destory", 2000).show(); 75 super.onDestroy(); 76 } 77 78 }
第二个activity(对话框风格)
1 package good.go; 2 3 import android.app.*; 4 import android.os.*; 5 import android.widget.Toast; 6 7 public class num extends Activity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 // TODO Auto-generated method stub 12 Toast.makeText(getApplicationContext(), "num --on create", 2000).show(); 13 super.onCreate(savedInstanceState); 14 } 15 16 @Override 17 protected void onStart() { 18 // TODO Auto-generated method stub 19 Toast.makeText(getApplicationContext(), "num --on start", 2000).show(); 20 super.onStart(); 21 } 22 23 @Override 24 protected void onRestart() { 25 // TODO Auto-generated method stub 26 Toast.makeText(getApplicationContext(), "num --on restart", 2000) 27 .show(); 28 super.onRestart(); 29 } 30 31 @Override 32 protected void onResume() { 33 // TODO Auto-generated method stub 34 Toast.makeText(getApplicationContext(), "num --on resume", 2000).show(); 35 super.onResume(); 36 } 37 38 @Override 39 protected void onPause() { 40 // TODO Auto-generated method stub 41 Toast.makeText(getApplicationContext(), "num --on pause", 2000).show(); 42 super.onPause(); 43 } 44 45 @Override 46 protected void onStop() { 47 // TODO Auto-generated method stub 48 Toast.makeText(getApplicationContext(), "num --on stop", 2000).show(); 49 super.onStop(); 50 } 51 52 @Override 53 protected void onDestroy() { 54 // TODO Auto-generated method stub 55 Toast.makeText(getApplicationContext(), "num --on destroy", 2000) 56 .show(); 57 super.onDestroy(); 58 } 59 60 }
manifest
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="good.go" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk android:minSdkVersion="7" /> 8 9 <application 10 android:icon="@drawable/ic_launcher" 11 android:label="@string/app_name" > 12 <activity 13 android:name=".TestProcActivity" 14 android:label="@string/app_name" > 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 <activity 22 android:name=".num" 23 android:label="@string/app_name" 24 android:theme="@android:style/Theme.Dialog" > 25 </activity> 26 </application> 27 28 </manifest>
无论在一个activity中哪个函数中调用 startactivity,都会经历一个活动状态的变迁过程,而不是立即跳转到别的activity中执行,所以了解android的
生命周期的过程,对了解程序的执行和分布是很有意义的。
原图连接: http://blog.csdn.net/android_tutor/article/details/5772285
最好的是在写程序的时候 加上与调试相关的代码,这样能避免一些自己想不到的错误。 同时也能帮助自己理解生命周期这个概念。