偷来的图^_^
验证过程中定义了两个布局三个Activity重写了7个生命周期方法
代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.activitylife.MainActivity" tools:ignore="MergeRootFrame" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myclick" android:text="点我暂停"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myclick2" android:text="点我停止"/> </LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.activitylife.MainActivity$PlaceholderFragment" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
package com.example.activitylife; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private static String TAG = "ActivityLife"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(getApplicationContext(), "onCreate", Toast.LENGTH_LONG).show(); Log.i(TAG, "onCreate"); } public void myclick(View v){ startActivity(new Intent(this,otherActivity.class)); } public void myclick2(View v){ startActivity(new Intent(this,otherActivity2.class)); } @Override protected void onDestroy() { Toast.makeText(getApplicationContext(), "onDestroy", Toast.LENGTH_LONG).show(); Log.i(TAG, "onDestroy"); super.onDestroy(); } @Override protected void onPause() { Toast.makeText(getApplicationContext(), "onPause", Toast.LENGTH_LONG).show(); Log.i(TAG, "onPause"); super.onPause(); } @Override protected void onResume() { Toast.makeText(getApplicationContext(), "onResume", Toast.LENGTH_LONG).show(); Log.i(TAG, "onResume"); super.onResume(); } @Override protected void onStart() { Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show(); Log.i(TAG, "onStart"); super.onStart(); } @Override protected void onStop() { Toast.makeText(getApplicationContext(), "onStop", Toast.LENGTH_LONG).show(); Log.i(TAG, "onStop"); super.onStop(); } @Override protected void onRestart() { Toast.makeText(getApplicationContext(), "onRestart", Toast.LENGTH_LONG).show(); Log.i(TAG, "onRestart"); super.onRestart(); } }
package com.example.activitylife; import android.app.Activity; import android.os.Bundle; public class otherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); } }
package com.example.activitylife; import android.app.Activity; import android.os.Bundle; public class otherActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); } }
运行结果如下
04-22 23:39:58.424: I/ActivityLife(2958): onCreate 04-22 23:39:58.454: I/ActivityLife(2958): onStart 04-22 23:39:58.474: I/ActivityLife(2958): onResume 04-22 23:40:12.114: I/ActivityLife(2958): onPause 04-22 23:40:16.614: I/ActivityLife(2958): onResume 04-22 23:40:19.414: I/ActivityLife(2958): onPause 04-22 23:40:22.264: I/ActivityLife(2958): onStop 04-22 23:40:30.964: I/ActivityLife(2958): onRestart 04-22 23:40:31.114: I/ActivityLife(2958): onStart 04-22 23:40:31.154: I/ActivityLife(2958): onResume 04-23 00:13:47.884: I/ActivityLife(2958): onPause 04-23 00:13:52.044: I/ActivityLife(2958): onStop