Activity声明周期1

oncreate():在Activity对象第一次创建时调用

onStart():当Activity变得可见时调用该函数

onResume():当Activity开始准备于用户交互时调用该方法(即获得焦点)

onpause():当系统即将启动另外一个Activity时调用该函数

onstop():当Activity变得不可见时调用该函数

onDestroy();当Activity被销毁之前将会调用该函数

onRestart():当一个Activity再次启动之前将会调用该函数

FirstActivity.java

 1 import android.os.Bundle;

 2 import android.view.View;

 3 import android.view.View.OnClickListener;

 4 import android.widget.Button;

 5 import android.app.Activity;

 6 import android.content.Intent;

 7 

 8 public class FirstActivity extends Activity {

 9 

10     private Button myButton;

11     @Override

12     protected void onCreate(Bundle savedInstanceState) {

13         System.out.println("FirstAcvity ---> onCreate");

14         super.onCreate(savedInstanceState);

15         setContentView(R.layout.main);

16         myButton = (Button)findViewById(R.id.myButton);

17         myButton.setText("启动第二个Activity");

18         myButton.setOnClickListener(new ButtonOnClickListener());

19     }

20     

21     

22     @Override

23     protected void onDestroy() {

24         System.out.println("FirstAcvity --->onDestory");

25         super.onDestroy();

26     }

27     

28     

29     @Override

30     protected void onPause() {

31         System.out.println("FirstAcvity --->onPause");

32         super.onPause();

33     }

34     

35     @Override

36     protected void onRestart() {

37         System.out.println("FirstAcvity --->onRestart");

38         super.onRestart();

39     }

40     

41     

42     @Override

43     protected void onResume() {

44         System.out.println("FirstAcvity --->onResume");

45         super.onResume();

46     }

47 

48 

49     @Override

50     protected void onStart() {

51         System.out.println("FirstAcvity --->onStart");

52         super.onStart();

53     }

54 

55     

56     @Override

57     protected void onStop() {

58         System.out.println("FirstAcvity --->onStop");

59         super.onStop();

60     }

61 

62 

63     class ButtonOnClickListener implements OnClickListener{

64         public void onClick(View v) {

65             Intent intent = new Intent();

66             intent.setClass(FirstActivity.this,SecondActivity.class);

67             FirstActivity.this.startActivity(intent);

68         }

69         

70     }

71 }

 mian.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:orientation="vertical"

 4     android:layout_width="fill_parent"

 5     android:layout_height="fill_parent"

 6     >

 7 <TextView

 8     android:id="@+id/myTextView"

 9     android:layout_width="fill_parent"

10     android:layout_height="wrap_content"

11     />

12 <Button

13     android:id="@+id/myButton"

14     android:layout_width="fill_parent"

15     android:layout_height="wrap_content"

16     />

17 </LinearLayout>

 

SecondActivity.java

 1 import android.app.Activity;

 2 import android.os.Bundle;

 3 

 4 public class SecondActivity extends Activity {

 5     @Override

 6     protected void onCreate(Bundle savedInstanceState) {

 7         System.out.println("SecondActivity--->onCreate");

 8         super.onCreate(savedInstanceState);

 9         setContentView(R.layout.second);

10     }

11 

12     @Override

13     protected void onDestroy() {

14         System.out.println("SecondActivity--->onDestory");

15         super.onDestroy();

16     }

17 

18     @Override

19     protected void onPause() {

20         System.out.println("SecondActivity--->onPause");

21         super.onPause();

22     }

23 

24     @Override

25     protected void onRestart() {

26         System.out.println("SecondActivity--->onRestart");

27         super.onRestart();

28     }

29 

30     @Override

31     protected void onResume() {

32         System.out.println("SecondActivity--->onResume");

33         super.onResume();

34     }

35 

36     @Override

37     protected void onStart() {

38         System.out.println("SecondActivity--->onStart");

39         super.onStart();

40     }

41 

42     @Override

43     protected void onStop() {

44         System.out.println("SecondActivity--->onStop");

45         super.onStop();

46     }

47     

48     

49     

50 }

 

second.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:orientation="vertical" >

 6     

 7     

 8     <TextView

 9     android:id="@+id/textview"

10     android:layout_width="fill_parent"

11     android:layout_height="wrap_content"

12     android:text="huangjianfeng"

13     />

14 

15 </LinearLayout>

 

AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 3     package="com.mars.activity04"

 4     android:versionCode="1"

 5     android:versionName="1.0" >

 6 

 7     <uses-sdk

 8         android:minSdkVersion="4"

 9         android:targetSdkVersion="18" />

10 

11     <application

12         android:allowBackup="true"

13         android:icon="@drawable/ic_launcher"

14         android:label="@string/app_name"

15         android:theme="@style/AppTheme" >

16         <activity

17             android:name="com.mars.activity04.FirstActivity"

18             android:label="@string/app_name" >

19             <intent-filter>

20                 <action android:name="android.intent.action.MAIN" />

21 

22                 <category android:name="android.intent.category.LAUNCHER" />

23             </intent-filter>

24         </activity>

25         <activity android:name=".SecondActivity" android:label="@string/second"/>

26     </application>

27 

28 </manifest>

 

string.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Activity04</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

    <string name="second">第二个Activity</string>

</resources>

当启动应用程序时,启动了程序的第一个Activity,调用该Activity的onCreate(),onStart(),onResume()等方法

当启动应用程序的第二个Activity时,调用第一个Activity的onPause()和调用第二个Activity的onCreate(),onStart(),onResume()等方法,然后又调用第一个Activity的onStop()方法

当点击后退按钮回到第一个Activity的时候,调用第二个Activity的onPause()方法和调用第一个Activity的onRestart(),onStart(),onResume()等方法,然后调用第二个Activity的onStop()方法和onDestory()方法

你可能感兴趣的:(Activity)