修罗

activity关闭指定界面
例如:在首页跳转到登录首页,然后跳转到用户名登录界面,登录成功后,需要关闭最后两个界面:1,定义界面管理器,用来保...
扫描右侧二维码阅读全文
16
2023/03

activity关闭指定界面

例如:在首页跳转到登录首页,然后跳转到用户名登录界面,登录成功后,需要关闭最后两个界面:

image-20230316145615327.png

1,定义界面管理器,用来保存当前应用所有开启的界面。

public class MyActivityManager {
    /**
     * 实例
     */
    private static MyActivityManager instance;

    /**
     * 已经显示的界面
     * 之所以使用Set
     * 是因为他不会保存重复元素
     */
    private static Set<Activity> activities = new LinkedHashSet<>();

    /**
     * 获取实例
     *
     * @return
     */
    public static MyActivityManager getInstance() {
        if (instance == null) {
            instance = new MyActivityManager();
        }
        return instance;
    }

    /**
     * 添加界面
     *
     * @param activity
     */
    public void add(Activity activity) {
        activities.add(activity);
    }

    /**
     * 移除界面
     *
     * @param activity
     */
    public void remove(Activity activity) {
        activities.remove(activity);
    }

    /**
     * 关闭所有界面
     */
    public void clear() {
        //for (Activity oldActivity : activities) {
        //    oldActivity.finish();
        //
        //}

        //使用迭代器遍历
        //否则可能会发生并发修改列表异常
        //因为在onActivityDestroyed方法中
        //从列表移除界面
        Iterator<Activity> iterator = activities.iterator();
        while (iterator.hasNext()) {
            iterator.next().finish();
        }
    }

    /**
     * 关闭到主界面
     */
    public void finishToMain() {
        LinkedList<Activity> list = new LinkedList<>(activities);
        Iterator<Activity> iterator = list.descendingIterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            // 可以写其他Activity,达到关闭某个界面的功能
            if (activity instanceof MainActivity) {
                break;
            }
            activity.finish();
        }
    }

    /**
     * 关闭所有界面
     */
    public void finishAll() {
        LinkedList<Activity> list = new LinkedList<>(activities);
        Iterator<Activity> iterator = list.descendingIterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            activity.finish();
        }
    }
}

2,新建AppContext.java,界面创建,在界面管理器添加activity;界面销毁,删除界面管理器的activity

public class AppContext extends Application implements Application.ActivityLifecycleCallbacks {
    private MyActivityManager myActivityManager;


    @Override
    public void onCreate() {
        super.onCreate();
        // 注册界面声明周期监听
        registerActivityLifecycleCallbacks(this);
        myActivityManager = MyActivityManager.getInstance();
    }

    //region activity生命周期监听
    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
        myActivityManager.add(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        myActivityManager.remove(activity);
    }
    //endregion
}

3,将AppContext声明在清单文件:

image-20230316150126096.png

4,使用

// 关闭到主界面
MyActivityManager.getInstance().finishToMain();
Last modification:August 4th, 2023 at 04:55 am

Leave a Comment