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

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

一、啟動activity,然后按返回鍵退出。
onCreate()->onStart()->onResume()
onPause()->onStop()->onDestory()
二、啟動activity,按Home鍵顯示桌面,再點程序圖標進入應用程序。
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 this與Activity.this的區(qū)別
這篇文章主要介紹了 Android this與Activity.this的區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
如何通過Battery Historian分析Android APP耗電情況
Android 從兩個層面統(tǒng)計電量的消耗,分別為軟件排行榜及硬件排行榜。它們各有自己的耗電榜單,軟件排行榜為機器中每個 App 的耗電榜單,硬件排行榜則為各個硬件的耗電榜單。這兩個排行榜的統(tǒng)計是互為獨立,互不干擾的2021-06-06
android studio的使用sdk manager的方法
這篇文章主要介紹了android studio的使用sdk manager的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Android利用ScaleTransition實現(xiàn)吹氣球動畫
這篇文章主要為大家介紹了如何將利用ScaleTransition實現(xiàn)一個吹氣球的動畫,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04
Android onActivityResult和setResult方法詳解及使用
這篇文章主要介紹了Android onActivityResult和setResult方法詳解及使用的相關(guān)資料,這里提供實例,幫助大家學習理解,需要的朋友可以參考下2016-12-12
Android 性能優(yōu)化系列之bitmap圖片優(yōu)化
在日常開發(fā)的APP,大部分時候需要想用戶展示圖片信息,圖片最終對應Android中的Bitmap對象。而對于APP端來說Bitmap又是一個比較麻煩的問題,主要表現(xiàn)在Bitmap是非常占用內(nèi)存的對象,處理不當將導致APP運行卡頓甚至出現(xiàn)OOM2021-11-11
Android中ShapeableImageView使用實例詳解(告別shape、三方庫)
之前Google推送了文章,Android?Material組件1.2.0里面就有ShapeableImageView,不用像以前再寫shape,下面這篇文章主要給大家介紹了關(guān)于Android中ShapeableImageView使用的相關(guān)資料,需要的朋友可以參考下2022-09-09

