Standard 启动模式
页面跳转顺序
MainActivity -> StandardActivity -> StandardActivity -> StandardActivity
页面栈 示例图
任务栈中只存在MainActivity时
任务栈中存在MainActivity、StandardActivity
MainActivity -> StandardActivity
MainActivity -> StandardActivity -> StandardActivity
MainActivity -> StandardActivity -> StandardActivity -> StandardActivity
通过ActivityManager查看页面栈
通过以下函数获取任务栈ID 、栈中Activity数量、栈底Activity、栈顶Activity、栈中所有Activity
import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.util.Log
import java.util.LinkedList
class Util {
companion object {
private val tag = Util::class.simpleName
val inactiveActivities = LinkedList<Activity>()
@JvmStatic
fun getAllActivity(context: Context) {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager?
val taskList = activityManager!!.getRunningTasks(1) // todo 获取最近运行的1个任务
for (taskInfo in taskList) {
// todo 获取栈顶Activity
val topComponentName = taskInfo.topActivity
// todo 获取栈底Activity
val baseComponentName = taskInfo.baseActivity
// todo 获取栈中Activity 数量
val numActivity = taskInfo.numActivities
// todo 获取任务ID
val taskId = taskInfo.taskId
Log.i(tag, "$tag 当前任务栈...... 任务ID\t$taskId")
Log.i(tag, "$tag 当前任务栈...... 栈中Activity数量\t$numActivity")
if (baseComponentName != null) {
Log.i(tag, "$tag 当前任务栈...... 栈底Activity:\t${baseComponentName.className}")
}
if (topComponentName != null) {
Log.i(tag, "$tag 当前任务栈...... 栈顶Activity:\t${topComponentName.className}")
}
// todo 栈中所有Activity名称
val activities= getInactiveActivities()
Log.i(tag, "$tag 当前任务栈...... 所有Activity:\t$activities")
Log.i(tag, "$tag 当前任务栈.....................................................")
}
}
// todo 获取栈中所有Activity
@JvmStatic
fun getInactiveActivities(): List<Activity> {
return inactiveActivities.toList()
}
}
}
注册监听所有Activity的事件
Application.ActivityLifecycleCallbacks是Application中的一个接口,调用registerActivityLifecycleCallbacks方法即可完成注册。用于全局监听每个Activity的生命周期、判断app前后状态等作用。
import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.start.util.Util
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityResumed(activity: Activity) {
}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
// todo 当Activity创建时,添加到列表中
if (!Util.inactiveActivities.contains(activity)) {
Util.inactiveActivities.add(activity)
}
Util.getAllActivity(baseContext)
}
override fun onActivityStarted(activity: Activity) {}
override fun o