Android—Activity生命周期
我們提供的服務有:網(wǎng)站設計制作、網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、河南ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的河南網(wǎng)站制作公司
一、圖解
二、概念分析
當一個活動被創(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
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
//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
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/second_button" >
//AndroidManifest.xml 中注冊 SecondActivity
android:label="SecondActivity" android:theme="@android:style/Theme.Dialog"/> //string.xml
文章標題:AndroidActivity生命周期
分享路徑:http://weahome.cn/article/gepcis.html