Activity声明周期1

时间:2022-02-05 04:35:54

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

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

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

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

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

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

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

FirstActivity.java

 import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent; public class FirstActivity extends Activity { private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("FirstAcvity ---> onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setText("启动第二个Activity");
myButton.setOnClickListener(new ButtonOnClickListener());
} @Override
protected void onDestroy() {
System.out.println("FirstAcvity --->onDestory");
super.onDestroy();
} @Override
protected void onPause() {
System.out.println("FirstAcvity --->onPause");
super.onPause();
} @Override
protected void onRestart() {
System.out.println("FirstAcvity --->onRestart");
super.onRestart();
} @Override
protected void onResume() {
System.out.println("FirstAcvity --->onResume");
super.onResume();
} @Override
protected void onStart() {
System.out.println("FirstAcvity --->onStart");
super.onStart();
} @Override
protected void onStop() {
System.out.println("FirstAcvity --->onStop");
super.onStop();
} class ButtonOnClickListener implements OnClickListener{
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(FirstActivity.this,SecondActivity.class);
FirstActivity.this.startActivity(intent);
} }
}

mian.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

SecondActivity.java

 import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("SecondActivity--->onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
} @Override
protected void onDestroy() {
System.out.println("SecondActivity--->onDestory");
super.onDestroy();
} @Override
protected void onPause() {
System.out.println("SecondActivity--->onPause");
super.onPause();
} @Override
protected void onRestart() {
System.out.println("SecondActivity--->onRestart");
super.onRestart();
} @Override
protected void onResume() {
System.out.println("SecondActivity--->onResume");
super.onResume();
} @Override
protected void onStart() {
System.out.println("SecondActivity--->onStart");
super.onStart();
} @Override
protected void onStop() {
System.out.println("SecondActivity--->onStop");
super.onStop();
} }

second.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="huangjianfeng"
/> </LinearLayout>

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mars.activity04"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mars.activity04.FirstActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" android:label="@string/second"/>
</application> </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()方法