ActivityStack

object ActivityStack

Activity管理任务栈,内部通过Application.registerActivityLifecycleCallbacks实现,可以打开和关闭指定 Activity,关闭所有Activity,关闭除指定Activity外所有Activity,终止进程等功能.

Author

Ayvytr 's GitHub

Since

3.1.6

  1. 修改isForeground,适配Android12最后关闭的Activity默认不会onDestroy()的问题

  2. registerCallback增加第二个参数observer,回调判断Activity是否前台可见

  3. 适配:不停的按返回很快关闭多个Activity时,Activity关闭/销毁顺序和正常顺序相反的问题

2.5.1

Functions

finish
Link copied to clipboard
@JvmStatic
fun finish(clazz: Class<out Activity>)

关闭clazz Activity.

finishAll
Link copied to clipboard
@JvmStatic
fun finishAll()

关闭所有Activity.

finishAllAndKillApp
Link copied to clipboard
@JvmStatic
fun finishAllAndKillApp()

关闭所有页面后,终止进程。推荐使用这个方法终止进程.

finishAllExcept
Link copied to clipboard
@JvmStatic
fun finishAllExcept(clazz: Class<out Activity>)

关闭除clazz外的所有Activity.

finishAllExceptName
Link copied to clipboard
@JvmStatic
fun finishAllExceptName(name: String)

关闭除类名为name外的所有Activity.

finishAllExceptSimpleName
Link copied to clipboard
@JvmStatic
fun finishAllExceptSimpleName(name: String)

关闭除简单类名为name外的所有Activity.

finishByName
Link copied to clipboard
@JvmStatic
fun finishByName(name: String)

关闭类名为name的Activity.

finishBySimpleName
Link copied to clipboard
@JvmStatic
fun finishBySimpleName(name: String)

关闭简单类名为name的Activity.

finishCurrent
Link copied to clipboard
@JvmStatic
fun finishCurrent()

关闭当前Activity.

finishExceptTop
Link copied to clipboard
@JvmStatic
fun finishExceptTop()

关闭除顶部Activity外的其他所有activity.

getCurrentActivity
Link copied to clipboard
@JvmStatic
fun getCurrentActivity(): Activity?

获取当前Activity.

getRunningActivityCount
Link copied to clipboard
@JvmStatic
fun getRunningActivityCount(): Int

返回已启动的Activity数量.

isForeground
Link copied to clipboard
@JvmStatic
fun isForeground(): Boolean

判断Activity是否在前台

killApp
Link copied to clipboard
@JvmStatic
fun killApp()

强杀进程. 慎重使用:直接调用这个方法可能有app自动重新打开的问题.

registerCallback
Link copied to clipboard
@JvmStatic
fun registerCallback(app: Application, observer: (Boolean) -> Unit = { isForeground -> })

注册Activity Lifecycle callback.

start
Link copied to clipboard
@JvmStatic
fun start(intent: Intent)

通过intent启动Activity.

@JvmStatic
fun start(clazz: Class<out Activity>, map: Map<String, Serializable>? = null)

启动clazz,map是附带参数.

startAndFinishOthers
Link copied to clipboard
@JvmStatic
fun startAndFinishOthers(intent: Intent)

intent方式启动Activity,并且关闭其他所有Activity.

@JvmStatic
fun startAndFinishOthers(clazz: Class<out Activity>, map: Map<String, Serializable>? = null)

启动clazz, map是附带参数,并且关闭其他所有Activity.

unregisterCallback
Link copied to clipboard
@JvmStatic
fun unregisterCallback(app: Application)

取消注册Activity Lifecycle callback.