利用ActivityLifecycleCallbacks优雅的退出应用

前言

在android开发中我们都会遇到一个需求,那就是退出应用,那么我们都会退出所有的activity。
我们一般常用的方式有下面几种:

第一种方式

我们写一个BaseActivity,在activity的创建的时候添加当前activity到集合中,在activity销毁的时候把当前activity移除出activity集合。退出应用时直接遍历activity集合,然后分别调用activity的finish()方法。

package com.huangjie.exitapplication;

import android.app.Activity;

import java.util.ArrayList;

/**
 * 应用管理类
 */

public class ActivityManager {
    private static ArrayList<Activity> mActivityList;

    private ActivityManager() {
    }

    /**
     * 添加activity
     *
     * @param activity
     */
    public static void addActivity(Activity activity) {
        if (mActivityList == null) {
            mActivityList = new ArrayList<>();
        }
        mActivityList.add(activity);
    }

    /**
     * 移除activity
     *
     * @param activity
     */
    public static void removeActivity(Activity activity) {
        mActivityList.remove(activity);
    }

    /**
     * 退出应用
     */
    public static void exit() {
        for (Activity activity : mActivityList) {
            activity.finish();
        }
        mActivityList.clear();
    }
}

package com.huangjie.exitapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

/**
 * Activity基类
 */

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        ActivityManager.removeActivity(this);
        super.onDestroy();
    }
}

第二种方式广播式

BaseActivity中注册一个广播接收器,在广播接收器中finish掉当前activity。所有的Activity继承BaseActivity。

package com.huangjie.exitapplication;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;


public class BaseActivity extends Activity {
    private static String ACTION = "activity_exit";

    private BroadcastReceiver mExitReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION);
        registerReceiver(mExitReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mExitReceiver);
        super.onDestroy();
    }
}

还有杀进程的方式。抛异常方式等等,这些都不够优雅,第一种方式如果当一个activity异常退出,是不会回掉onDestroy()方法,也就意味我们静态Activity集合无法移除当前activity,会导致内存泄漏。android 系统为每个应用分配的内存都是有限的,如果内存泄漏到一定程度就会导致应用崩溃。

第三种方式

ActivityLifecycleCallbacks 这个接口看着名字大概都能猜出来他是activity生命周期回掉接口。

public interface ActivityLifecycleCallbacks {
        void onActivityCreated(Activity activity, Bundle savedInstanceState);
        void onActivityStarted(Activity activity);
        void onActivityResumed(Activity activity);
        void onActivityPaused(Activity activity);
        void onActivityStopped(Activity activity);
        void onActivitySaveInstanceState(Activity activity, Bundle outState);
        void onActivityDestroyed(Activity activity);
    }

当我们一旦给应用注册了ActivityLifecycleCallbacks,那么上面接口中对应的方法都是应用中所有activity的生命周期都会回掉的方法。不管是我们写的activity,还是我们引用的第三方库中activity的生命周期都会回掉这些方法。那么我们我们可以在这里做处理。

package com.huangjie.exitapplication;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;



public class MyApplication extends Application {
    private ActivityLifecycleCallbacks mcallBack;
    private static ArrayList<Activity> mActiivtyList;

    @Override
    public void onCreate() {
        super.onCreate();
        initLifeCallBack();
        registerActivityLifecycleCallbacks(mcallBack);
    }


    private void initLifeCallBack() {
        mcallBack = new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                if (mActiivtyList == null) {
                    mActiivtyList = new ArrayList<>();
                }
                mActiivtyList.add(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

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

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                mActiivtyList.remove(activity);
            }
        };
    }


    /**
     * 退出应用程序
     */
    public static void exit() {
        for (Activity activity : mActiivtyList) {
            activity.finish();
        }
        mActiivtyList.clear();
    }

}

    原文作者:huangandroid
    原文地址: https://www.jianshu.com/p/1845e81933ef
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞