Android中自定义activity管理类

网友投稿 1016 2022-10-04

Android中自定义activity管理类

Android中自定义activity管理类

Android中自定义activity管理类

​​编者:李国帅​​

​​时间:2020/11/26  ​​

目录

​​Android中自定义activity管理类​​

​​背景:​​

​​主要函数:​​

​​管理类:​​

​​应用,创建一个基类包装对应功能:​​

背景:

所需资源:android

2018年的时候,为了解决如下问题而自定义了一个activity管理类:

当关闭apk的时候关闭所有的activity获取顶部activity获取mainActivity或其他指定类关闭出指定activity之外的其他activity

主要函数:

private static MyApplication instance;
private Context context;
private Map<String, Activity> activityMap = new HashMap<String, Activity>();

@Override
public void onCreate() {
    super.onCreate();
    srcTable = new ArrayList<HashMap<String, Object>>();
}

// 添加Activity 到容器
public void addActivity(String className, Activity activity) {
    activityMap.put(className, activity);
}

// 遍历所有Activity 并finish
public void exit() {
    for (Entry<String, Activity> entry : activityMap.entrySet()) {
        entry.getValue().finish();
    }
    System.exit(0);
}

管理类:

import java.util.LinkedList;

import java.util.List;



import android.app.Activity;



/**

* @author liguoshuai

* @category 页面管理

*/

public class AppActivityList {

private List<Activity> activityList = new LinkedList<Activity>();



// 添加Activity 到容器中

public void addActivity(Activity newActivity) {

for (Activity activity : activityList) {

boolean same = activity.toString().contentEquals(newActivity.toString());

if (same) {

activityList.remove(activity);

break;

}

}

activityList.add(newActivity);// 重新加入到尾部

}



public Activity getTopActivity() {

Activity topActivity = null;

int lastPos = activityList.size() - 1;

if (lastPos >= 0) {

topActivity = activityList.get(lastPos);

}

return topActivity;

}



public void finishAll() {

for (Activity activity : activityList) {

if (activity != null) {

activity.finish();

}

}

}



public void resetActivity(Activity except) {

for (Activity activity : activityList) {

boolean same = activity.toString().contentEquals(except.toString());

if (!same) {

if (activity != null) {

activity.finish();

}

}

}

}



public Activity getMainActivity() {

for (Activity activity : activityList) {

if (activity.getClass().getSimpleName().contentEquals("MainActivity")) {// singleton

return activity;

}

}

return null;

}



public Activity getLoginActivity() {

for (Activity activity : activityList) {

if (activity.getClass().getSimpleName().contentEquals("LoginActivity")) {// singleton

return activity;

}

}

return null;

}



public void removeActivity(Activity removeActivity) {

int index = 0;

for (Activity activity : activityList) {

boolean same = activity.toString().contentEquals(removeActivity.toString());

if (same) {

break;

}

index++;

}

activityList.remove(index);



}



public void removeActivity(String activityName) {

for (Activity activity : activityList) {

if (activity == null)

continue;

String className = activity.getClass().getSimpleName();

if (className.contentEquals(activityName)) {

activity.finish();

}

}

}

}

应用,创建一个基类包装对应功能:

import android.app.Activity;import android.content.pm.ActivityInfo;import android.content.res.Configuration;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.app.FragmentActivity;/** * @author liguoshuai @category */public class BaseActivity extends FragmentActivity implements Handler.Callback { public BaseActivity thisActivity; protected Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); thisActivity = this;// 窗口上下文 mHandler = new Handler(this); MyApplication.getActivityList().addActivity(this);// 1 } @Override protected void onDestroy() { // MyLog.i(TAG, " destroy!"); MyApplication.getActivityList().removeActivity(this);// 1 super.onDestroy(); // Always call the superclass } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else { } } @Override public void onResume() { SoftInput.hideSoftInput(thisActivity); super.onResume(); } // 实现消息处理 public Handler getMsgHandler() { return mHandler; } @Override public boolean handleMessage(Message msg) { switch (msg.what) { } return false; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Lombok和MapStruct整合详情
下一篇:小程序中搜索功能的实现方法(代码)(小程序搜索框简单的实现)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~