标签:
关于生命周期图资源很多,不再粘贴。从代码运行来看看Activity的生命周期。
1 package com.example.day14_01activitylifecycle; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 9 public class MainActivity extends Activity { 10 public static final String TAG = "day14_01activitylifecycle"; 11 public void click(View v){ 12 Intent intent = new Intent(); 13 intent.setClass(this, SecondActivity.class); 14 startActivity(intent); 15 } 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 Log.i(TAG, "onCreate()"); 21 } 22 @Override 23 protected void onStart() { 24 // TODO Auto-generated method stub 25 super.onStart(); 26 Log.i(TAG, "onStart()"); 27 } 28 @Override 29 protected void onResume() { 30 // TODO Auto-generated method stub 31 super.onResume(); 32 Log.i(TAG, "onResume()"); 33 34 } 35 @Override 36 protected void onRestart() { 37 // TODO Auto-generated method stub 38 super.onRestart(); 39 Log.i(TAG, "onRestart()"); 40 41 } 42 @Override 43 protected void onPause() { 44 // TODO Auto-generated method stub 45 super.onPause(); 46 Log.i(TAG, "onPause()"); 47 } 48 @Override 49 protected void onStop() { 50 // TODO Auto-generated method stub 51 super.onStop(); 52 Log.i(TAG, "onStop()"); 53 } 54 @Override 55 protected void onDestroy() { 56 // TODO Auto-generated method stub 57 super.onDestroy(); 58 Log.i(TAG, "onDestroy()"); 59 } 60 }
1 package com.example.day14_01activitylifecycle; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class SecondActivity extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 // TODO Auto-generated method stub 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 } 14 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.day14_01activitylifecycle.MainActivity" > 10 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="@string/hello_world" /> 15 16 17 <Button 18 android:id="@+id/bt" 19 android:onClick="click" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="click me " 23 android:layout_centerHorizontal="true"/> 24 25 <EditText 26 android:id="@+id/et" 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content" 29 android:layout_centerHorizontal="true" 30 android:layout_below="@id/bt"/> 31 </RelativeLayout>
1,点击进入该app界面
LogCat将打印出以下代码,可以看见Activity执行了OnCreate(),Onstart(),OnResume()
2,点击click me Button,进入以下界面
LogCat将打印出以下代码,可以看见Activity执行了OnPause()(针对第一个Activity)
3,按返回键返回到第一个Activity
LogCat将打印出以下代码,可以看见Activity执行了OnResume()
4,按返回键退出程序
LogCat将打印出以下代码,可以看见Activity执行了OnReuse(), OnStop(), OnDestory()
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4526770.html