在开发app的时候,很多时候我们都需要管理运行的 activity,比如退出登陆后关闭所有activity,只保留登陆页面等等,一般常见的是继承父类activity,然后里面有个list add 和 remove 当前activity,但是感觉这种比较繁琐,我要说的一下2种
方法一:ActivityLifecycleCallbacks收集
可以在自定义application的oncreate注册activity生命周期监听
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks{
override fun onActivityPaused(p0: Activity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityStarted(p0: Activity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityDestroyed(p0: Activity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityStopped(p0: Activity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityResumed(p0: Activity) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
这是kotlin写法,当然这里只是简单的列举,最好的当然是单独写一个类继承ActivityLifecycleCallbacks,然后在里面管理activity
object ActivitysManager : Application.ActivityLifecycleCallbacks {
private val activityList = LinkedHashMap<KClass<out Activity>, Activity>()
override fun onActivityPaused(p0: Activity) {
}
override fun onActivityStarted(p0: Activity) {
}
override fun onActivityDestroyed(p0: Activity) {
activityList.remove(p0::class)
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
}
override fun onActivityStopped(p0: Activity) {
}
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
activityList.put(p0::class,p0)
}
override fun onActivityResumed(p0: Activity) {
}
/**
* 如果不想finish可以添加对应
* activity的class
*/
fun finishActivity(vararg without: KClass<out Activity>? = arrayOf()){
for (entry in activityList) {
if (without.contains(entry.key)){
continue
}
entry.value.finish()
}
}
/**
* 完全退出
*/
fun exit(){
finishActivity()
System.exit(0);
}
}
然后在加入监听registerActivityLifecycleCallbacks(ActivitysManager),好处就是变成一个工具类,放到任何项目中都行,不用去修改基类
方法二:反射
虽然说反射比较骚,但是保不定那天google把方法改了就哦豁了
object ActivityStackManager {
//直接获取app 整个activity栈,避免activity的各种启动模式
fun finishActivity(vararg without: KClass<out Activity>? = arrayOf()) {
try {
//ActivityThread里持有了一个Map,这个Map的Value是ActivityClientRecord
val activityThread = Class.forName("android.app.ActivityThread")
val currentActivityThread = activityThread.getDeclaredMethod("currentActivityThread")
currentActivityThread.isAccessible = true
//获取主线程对象
val activityThreadObject = currentActivityThread.invoke(null)
val mActivitiesField = activityThread.getDeclaredField("mActivities")
mActivitiesField.isAccessible = true
//ActivityClientRecord是持有一个Activity对象的,然后相当于是一个Activity被一个Map持有
//源码ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
val mActivities = mActivitiesField.get(activityThreadObject) as Map<Any, Any>
for ((_, value) in mActivities) {
val activityClientRecordClass = value.javaClass
val activityField = activityClientRecordClass.getDeclaredField("activity")
activityField.isAccessible = true
val activity = activityField.get(value) as Activity
//如果包含就跳过
if (without.contains(activity::class)) {
continue
}
activity.finish()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}