您好,登錄后才能下訂單哦!
Android—Activity生命周期
一、圖解
二、概念分析
當一個活動被創(chuàng)建時,調(diào)用onCreate() 方法
當一個活動能被用戶看到(即可見)時,調(diào)用onStart() 方法
當一個活動可獲取用戶焦點時,調(diào)用onResume() 方法
所以打開一個活動時,會先后執(zhí)行onCreate()->onStart()->onResume三個方法
當一個活動不能獲取用戶焦點時(此時另一個活動進入前臺),調(diào)用onPause() 方法
當一個活動不可見(即被其他活動完全覆蓋)時,調(diào)用onStop() 方法
當我們準備啟動另一個新的活動覆蓋當前活動時時,會依次調(diào)用當前活動類的
onPause() -> onStop() 方法
當我們再次啟動該活動,該活動由后臺轉(zhuǎn)到前臺運行時,會依次調(diào)用該活動類的onRestart()->onStart()->onResume()三個方法
當我們退出程序,或調(diào)用finish() 方法時,則依次調(diào)用 onPause() -> onStop() -> onDestroy() 方法
三、例子說明
//FirstActivity.java
package mars.activity05;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("FirstActivity ---> onCreate ");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onStop");
super.onStop();
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data", "Hello I'm FirstActivity!");
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_button"
/>
</LinearLayout>
//SecondActivity.java
package mars.activity05;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity{
private Button secondButton;
TextView text = null;
String str = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("SecondActivity--->onCreate");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
secondButton = (Button)findViewById(R.id.secondButton);
secondButton.setOnClickListener(new ButtonListener());
text = (TextView) findViewById (R.id.text);
Intent intent = getIntent();
str = intent.getStringExtra("extra_data");
text.setText(str);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onStop");
super.onStop();
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SecondActivity.this, FirstActivity.class);
SecondActivity.this.startActivity(intent);
}
}
}
//second.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="wrap_content"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_button"
></TextView>
<Button
android:id="@+id/secondButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_button"
/>
</LinearLayout>
//AndroidManifest.xml 中注冊 SecondActivity
<activity android:name=".SecondActivity"
android:label="SecondActivity"
android:theme="@android:style/Theme.Dialog"/>
//string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, FirstActivity!</string>
<string name="app_name">activity05</string>
<string name="first_button">啟動第二個Activity</string>
<string name="second_button">回到第一個Activity</string>
</resources>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。