Android Activity的生命周期與啟動模式全面解讀
Activity概述
• Activity(活動)是Android應(yīng)用程序中最基本的組成單位。
• Activity主要負責創(chuàng)建顯示窗口,一個Activity對象通常就代表了一個單獨的屏幕。
• Activity是用戶唯一可以看得到的組件,用來與用戶進行交互的。
• Activity是通過一個Activity棧來進行管理,當前顯示的Activity被放到棧頂。
Activity生命周期

生命周期的調(diào)用順序

一、啟動activity,然后按返回鍵退出。
onCreate()->onStart()->onResume()
onPause()->onStop()->onDestory()
二、啟動activity,按Home鍵顯示桌面,再點程序圖標進入應(yīng)用程序。
onCreate()->onStart()->onResume()
onPause()->onStop()
onRestart()->onStart()->onResume()
三、啟動activityA,再啟動activityB
AonCreate()->AonStart()->AonResume()
AonPause()
BonCreate()->BonStart()->BonResume()
演示
package com.qingsu.yingguday06;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mBtIntent,mBtIntentOne,mBtIntentTwo,mBtIntentThree;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("TAG","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mBtIntent.setOnClickListener(this);
}
private void initView(){
mBtIntent = findViewById(R.id.bt_intentmain);
}
@Override
protected void onStart() {
Log.d("TAG","onStart");
super.onStart();
}
@Override
protected void onResume() {
Log.d("TAG","onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.d("TAG","onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.d("TAG","onStop");
super.onStop();
}
@Override
protected void onRestart() {
Log.d("TAG","onRestart");
super.onRestart();
}
@Override
protected void onDestroy() {
Log.d("TAG","onDestroy");
super.onDestroy();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_intentmain:
startActivity(intent);;
break;
default:
break;
}
}
}
package com.qingsu.yingguday06;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class HomeActivity extends AppCompatActivity {
Button mBtHome,mBtIntentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TAG","BonCreate");
setContentView(R.layout.activity_home);
setTitle("頁面B");
mBtHome = findViewById(R.id.bt_intenthome);
mBtIntentB = findViewById(R.id.bt_intentB);
mBtHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
}
});
mBtIntentB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeActivity.this,HomeActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
Log.d("TAG","BonStart");
super.onStart();
}
@Override
protected void onResume() {
Log.d("TAG","BonResume");
super.onResume();
}
@Override
protected void onPause() {
Log.d("TAG","BonPause");
super.onPause();
}
@Override
protected void onStop() {
Log.d("TAG","BonStop");
super.onStop();
}
@Override
protected void onRestart() {
Log.d("TAG","BonRestart");
super.onRestart();
}
@Override
protected void onDestroy() {
Log.d("TAG","onDestroy");
super.onDestroy();
}
}
Activity的啟動模式
standard(默認standard)、singleTop、singleTask、singleInstance
啟動模式的設(shè)置
清單文件中在活動聲明中加入launchMode屬性 默認為standard方式
android:launchMode="singleTask"
<activity android:name=".MainActivity"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
standard(默認standard)
一直入棧 一直創(chuàng)建新的Activity

singleTop
當前頁面中創(chuàng)建當前頁面不會新創(chuàng)建Activity

singleTask
當Activity存在則會將其置頂,Activity上面的棧則會被彈出!即Activity上面的Activity會銷毀!

singleInstance
每一個Activity都是唯一的 當Activity存在不會新建新的Activity

到此這篇關(guān)于Android Activity的生命周期與啟動模式全面解讀的文章就介紹到這了,更多相關(guān)Android Activity 啟動模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android studio實現(xiàn)簡單的計算器(無bug)
這篇文章主要為大家詳細介紹了android studio實現(xiàn)簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
Android筆記之:App調(diào)試的幾個命令的實踐與分析
本篇文章介紹了,在Android中:App調(diào)試的幾個命令的實踐與分析。需要的朋友參考下2013-04-04
Android應(yīng)用自動跳轉(zhuǎn)到應(yīng)用市場詳情頁面的方法
最近在工作中遇到一個需求,推廣部門要求實現(xiàn)應(yīng)用自動跳轉(zhuǎn)到應(yīng)用市場詳情頁面,通過查找一些資料,實現(xiàn)出來了,覺得有必要整理下方便以后或者有需要的朋友們參考借鑒,下面來一起詳細看看Android應(yīng)用自動跳轉(zhuǎn)到應(yīng)用市場詳情頁面的方法吧。2016-12-12

