关于Android 退出登录与强制下线功能记录
一、前提介绍
1.退出登录功能:退出登录到用户登录界面
2.强制下线功能:退出登录到用户登录界面,不清除用户登录帐号密码
出现问题:两个功能同样代码,在退出登录正常,强制下线报msg's executing time is too long,最后解决办法如下
二、Activity管理类
class ActivityManager {
private var activityStack:Stack<Activity>?= null// activity栈
// 把一个activity压入栈中
fun pushOneActivity(actvity:Activity) {
if (activityStack == null) {
activityStack = Stack()
}
activityStack!!.add(actvity)
}
// 移除一个activity
fun popOneActivity(activity:Activity?) {
if (activityStack != null && activityStack!!.size > 0) {
if (activity!= null) {
activity.finish()
activityStack!!.remove(activity)
}
}
}
// 退出所有activity
fun finishAllActivity() {
if (activityStack != null) {
while (activityStack!!.size > 0) {
activityStack!!.pop().finish()
}
}
}
/**
* 结束除指定类名的所有Activity
*/
fun finishExcludeActivityAllActivity(cls:Class<*>) {
for (activityin this.activityStack!!) {
if (activity== null) continue
if (activity.javaClass == cls) continue
popOneActivity(activity)
}
}
companion object {
private var instance:ActivityManager?= null
// 单例模式
fun getInstance():ActivityManager {
if (instance == null) {
instance = ActivityManager()
}
return instance as ActivityManager
}
}
}
三、退出登录功能
退出登录按钮点击执行
//首先清除用户保存的数据
SPUtils.remove(Constant.SPConstant.CUR_USER_NAME)
SPUtils.remove(Constant.SPConstant.CUR_USER_SESSIONID)
SPUtils.remove(Constant.SPConstant.CUR_USER_ID)
Thread.sleep(500)
Log.e("登录2",LoginUser.name)
ActivityManager.getInstance().finishAllActivity()
startActivity(Intent(this, LoginActivity::class.java))
四、强制下线功能
强制下线功能是根据数据库唯一的sessionid来判断,以用户最后一次登录为准,强制下线采用广播通知。
<activity
android:name=".activity.LoginActivity"
android:launchMode="singleTop" //这块需要注意
android:screenOrientation="portrait"
android:theme="@style/SplashFullWindowTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
LoginActivity界面跳转到别的界面,不使用finish()直接跳转,同时在界面初始化时需要添加
//销掉出登录界面外其余界面,防止第二次进入
ActivityManager.getInstance().finishExcludeActivityAllActivity(this::class.java)
强制下线弹出框中点击重新登录按钮执行
// 点击按钮所调用的方法
SPUtils.remove(Constant.SPConstant.CUR_USER_NAME)
SPUtils.remove(Constant.SPConstant.CUR_USER_SESSIONID)
SPUtils.remove(Constant.SPConstant.CUR_USER_ID)
startActivity(Intent(this@BaseMvpActivity, LoginActivity::class.java))
补充测试时会发现新的错误:java.util.ConcurrentModificationException异常
新的ActivityManager如下:
class ActivityManager {
private var activityStack:Stack<Activity>?= null// activity栈
/**
* 将activity推入栈内
*
* @paramactivity
*/
fun pushOneActivity(activity:Activity) {
if (activityStack == null) {
activityStack = Stack()
}
activityStack!!.add(activity)
}
/**
* 将activity移出栈
*
* @paramactivity
*/
fun popOneActivity(activity:Activity?) {
if (activity!= null) {
activityStack!!.remove(activity)
}
}
/**
* 结束指定activity
*
* @paramactivity
*/
@Suppress("NAME_SHADOWING")
fun endActivity(activity:Activity?) {
val activity= activity
if (activity!= null) {
activity.finish()
activityStack!!.remove(activity)
}
}
/**
* 获得当前的activity(即最上层)
*
* @return
*/
fun currentActivity():Activity?{
var activity:Activity?= null
if (!activityStack!!.empty())
activity= activityStack!!.lastElement()
return activity
}
/**
* 弹出除cls外的所有activity
*
* @paramcls
*/
fun popAllActivityExceptOne(cls:Class<out Activity>) {
while (true) {
val activity= currentActivity() ?:break
if (activity.javaClass == cls) {
break
}
popOneActivity(activity)
}
}
/**
* 结束除cls之外的所有activity,执行结果都会清空Stack
*
* @paramcls
*/
fun finishAllActivityExceptOne(cls:Class<out Activity>) {
while (!activityStack!!.empty()) {
val activity= currentActivity()
if (activity!!.javaClass == cls) {
popOneActivity(activity)
} else {
endActivity(activity)
}
}
}
/**
* 结束所有activity
*/
fun finishAllActivity() {
while (!activityStack!!.empty()) {
val activity= currentActivity()
endActivity(activity)
}
}
companion object {
private var instance:ActivityManager?= null
// 单例模式
fun getInstance():ActivityManager {
if (instance == null) {
instance = ActivityManager()
}
return instance as ActivityManager
}
}
}
在登录页面使用
//销掉出登录界面外其余界面,防止第二次进入
ActivityManager.getInstance().finishAllActivityExceptOne(this::class.java)
其余没变
完结