其余的方法应该在业务中调用,比如:
-
在输入完账号密码后登录进入首页后,调用 finishBeForActivity 方法来关闭首页之前的页面
-
在退出登录操作时候又要回到登录页,此时在登录页调用 finishBeForActivity 方法来关闭之前的页面
-
…(不再举例)
使用案例:
//根据类名关闭指定的Activity
ActivityManager.get().finishActivity(“MainActivity”)
ActivityManager.get().finishActivity(“MainActivity”, “MainActivity”)
ActivityManager.get().finishActivity(“MainActivity”, “MainActivity”, “MainActivity”)
//关闭指定的Activity
Activi
tyManager.get().finishActivity(this)
ActivityManager.get().finishActivity(this, this)
ActivityManager.get().finishActivity(this, this, this)
//关闭所有Activity
ActivityManager.get().finishAllActivity()
//关闭某Activity之前所有的Activity, 一般触发登录或切换账号操作才会使用
ActivityManager.get().finishBeForActivity(this)
/**
-
栈管理Activity
-
@author: Cyn
-
@date: 2021/7/16
*/
@Suppress(“NAME_SHADOWING”)
class ActivityManager private constructor() {
companion object {
private var activityTaskList: MutableList? = null
private var instance: ActivityManager? = null
get() {
if (null == field) {
field = ActivityManager()
activityTaskList = mutableListOf()
}
return field
}
@Synchronized
fun get(): ActivityManager {
return instance!!
}
}
/**
- 添加一个Activity
*/
fun addActivity(activity: Activity) {
activityTaskList?.add(activity)
}
/**
- 删除某个Activity
*/
fun removeActivity(activity: Activity? = null) {
if (null != activity) {
activityTaskList?.remove(activity)
}
}
/**
-
关闭指定 Activity
-
@param activity 指定要关闭的 Activity
*/
fun finishActivity(vararg activity: Activity) {
activityTaskList?.let {
for (i in activity.indices) {
for (j in 0 until it.size) {
if (it == activity[i]) {
when (it[j]) {
is BaseActivity -> (it[j] as BaseActivity).finish()
else -> it[j].finish()
}
}
}
}
}
}
/**
-
根据类名关闭指定Activity
-
一般调用BaseActivity中的finish方法即可,留作备用
-
@param activityName 指定要关闭的 Activity 类名字
*/
fun finishActivity(vararg activityName: String) {
activityTaskList?.let {
for (i in activityName.indices) {
for (j in 0 until it.size) {
if (it[j].javaClass.name.contains(activityName[i])) {
when (it[j]) {
is BaseActivity -> (it[j] as BaseActivity).finish()
else -> it[j].finish()
}
}
}
}
}
}
/**
- 关闭所有Activity
*/
fun finishAllActivity() {
activityTaskList?.let {
for (i in 0 until it.size) {
when (it[i]) {
is BaseActivity -> (it[i] as BaseActivity).finish()