清空所有Activity

退出APP是需要清空所有Activity, 清空的前提是 需要记录已开启的所有 Activity。
在Application.ActivityLifecycleCallbacks的生命周期中调用add和remove操作。

public class ActivityStackHelper {
    private static LinkedHashMap<Class<?>, Activity> activities = new LinkedHashMap<>();

    /**
     * 保存activity到集合中
     *
     * @param activity
     */
    public static void add(Activity activity) {
        if (activity == null) {
            return;
        }
        activities.put(activity.getClass(), activity);
    }

    /**
     * 判断指定的activity在map中是否存在
     *
     * @param clazz 指定的activity
     */
    public static boolean isActivityExist(Class clazz) {
        return clazz != null && get(clazz) != null;
    }

    /**
     * 获取指定clazz的activity
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends Activity> T get(Class<T> clazz) {
        return (T) activities.get(clazz);
    }

    /**
     * 获取当前集合中的activity
     *
     * @return
     */
    public static int getSize() {
        return activities.size();
    }

    /**
     * 指定activity是否是activity栈中最底层的activity
     *
     * @param activity
     * @return true,是;false,否
     */
    public static boolean isLastActivity(Activity activity) {
        if (activity == null) {
            return false;
        }
        if (activities == null || activities.size() == 0) {
            return false;
        }

        ListIterator<Map.Entry<Class<?>, Activity>> iterator =
                new ArrayList<>(activities.entrySet()).listIterator(activities.size());

        while (iterator.hasPrevious()) {
            Map.Entry<Class<?>, Activity> next = iterator.previous();
            if (next.getKey() == activity.getClass()) {
                return !iterator.hasPrevious();
            }
        }

        return false;
    }

    /**
     * 移除集合中的activity
     *
     * @param activity
     */
    public static void remove(Activity activity) {
        if (activity == null) {
            return;
        }
        activities.remove(activity.getClass());
    }

    /**
     * 清空并finish所有activity
     */
    public static void clearAndFinishActivity() {
        Set<Map.Entry<Class<?>, Activity>> entries = activities.entrySet();
        for (Map.Entry<Class<?>, Activity> entry : entries) {
            Activity activity = entry.getValue();
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        activities.clear();
    }

    /**
     * 清空并finish所有activity(除指定activity之外)
     *
     * @param clazzs
     */
    public static void clearAndFinishOtherActivity(Class... clazzs) {

        List<Class> cls = clazzs != null ? Arrays.asList(clazzs) : null;
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Class<?>, Activity> next = iterator.next();
            Class<?> key = next.getKey();
            if (cls != null && cls.contains(key)) {
                continue;
            }
            Activity value = next.getValue();
            if (!value.isFinishing()) {
                value.finish();
            }
            iterator.remove();
        }
    }

    /**
     * 指定activity是否在运行
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends Activity> boolean isActivityRunning(Class<T> clazz) {
        T activity = get(clazz);
        if (activity == null || activity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取当前栈顶activity
     *
     * @return
     */
    public static Activity getTopActivity() {
        if (activities == null || activities.size() == 0) {
            return null;
        }
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        Map.Entry<Class<?>, Activity> next = null;
        while (iterator.hasNext()) {
            next = iterator.next();
        }
        return next != null ? next.getValue() : null;
    }


    /**
     * 获取次栈顶activity
     *
     * @return
     */
    public static Activity getTopSecondActivity() {
        if (activities == null || activities.size() == 0) {
            return null;
        }
        int size = activities.size();
        if (size < 2){
            return null;
        }
        int index = 1;
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        Map.Entry<Class<?>, Activity> next = null;
        while (iterator.hasNext()) {
            next = iterator.next();
            if (index == size - 1){
                if (next != null){
                    LogUtils.i("ActivityStackHelper", "secondActivity" + next.getValue());
                    return next.getValue();
                }
            }
            index++;
        }
        return null;
    }

    /**
     * 获取除了指定activity之外的栈顶activity
     *
     * @param clazz
     * @return
     */
    public static Activity getTopActivityExclude(Class clazz) {
        if (activities == null || activities.size() == 0) {
            return null;
        }
        Iterator<Map.Entry<Class<?>, Activity>> iterator = activities.entrySet().iterator();
        Map.Entry<Class<?>, Activity> resule = null;
        while (iterator.hasNext()) {
            Map.Entry<Class<?>, Activity> next = iterator.next();
            if (next.getKey() != clazz) {
                resule = next;
            }
        }
        return resule != null ? resule.getValue() : null;
    }

}

调用使用方法

public class AppActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        ActivityStackHelper.add(activity);
    }
    @Override
    public void onActivityDestroyed(Activity activity) {
        ActivityStackHelper.remove(activity);
    }
  }

上述工具类用到了LinkedHashMap 是为了保证顺序,其特性详见 https://blog.csdn.net/iblade/article/details/108369770

如果您想在登录activity中实现清空密码fragment账号和验证码fragment账号,可以在清空密码输入框的同时清空验证码输入框和账号输入框。您可以在您的登录activity中获取验证码fragment和密码fragment的实例,并调用它们的清空方法。以下是示例代码: ```java public class LoginActivity extends AppCompatActivity { private AccountFragment accountFragment; private PasswordFragment passwordFragment; private VerifyCodeFragment verifyCodeFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // 获取账号、密码和验证码的fragment实例 accountFragment = (AccountFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_account); passwordFragment = (PasswordFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_password); verifyCodeFragment = (VerifyCodeFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_verify_code); } // 清空账号、密码和验证码输入框 private void clearInputFields() { if (accountFragment != null) { accountFragment.clearInputField(); } if (passwordFragment != null) { passwordFragment.clearInputField(); } if (verifyCodeFragment != null) { verifyCodeFragment.clearInputField(); } } // 点击清空按钮时调用此方法 public void onClearClick(View view) { clearInputFields(); } } ``` 在上面的示例代码中,我们定义了一个`clearInputFields()`方法,该方法会依次调用账号、密码和验证码fragment的`clearInputField()`方法,以清空它们的输入框。我们还在登录activity的布局文件中添加了一个清空按钮,当用户点击该按钮时,会调用`onClearClick()`方法,该方法会调用`clearInputFields()`方法来清空输入框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值