Android 五种退出应用的方法

一.使用RxBus监听:

原理:在BaseActivity 中都初始化RxBus,同时监听某种事件,当接收到这个事件的时候,就finish();————>其实就是类似广播监听事件

代码:

ublic class BaseActivity3 extends AppCompatActivity {
    Subscription mSubscription;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initRxBus();
    }
    //接收退出的指令,关闭所有activity
    private void initRxBus() {
        mSubscription = RxBus.getInstance().toObserverable(Event.class)
                .subscribe(event-{
                        finish();//监听到事件就finish
                    },
                     error->{
                     };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }
}
...
public class Event{

}


RxBus 进行单例

public class RxBus {

    private static volatile RxBus mInstance;

    private final Subject bus;


    public RxBus()
    {
        bus = new SerializedSubject<>(PublishSubject.create());
    }

    /**
     * 单例模式RxBus
     *
     * @return
     */
    public static RxBus getInstance()
    {

        RxBus rxBus2 = mInstance;
        if (mInstance == null)
        {
            synchronized (RxBus.class)
            {
                rxBus2 = mInstance;
                if (mInstance == null)
                {
                    rxBus2 = new RxBus();
                    mInstance = rxBus2;
                }
            }
        }

        return rxBus2;
    }


    /**
     * 发送消息
     *
     * @param object
     */
    public void post(Object object)
    {

        bus.onNext(object);

    }

    /**
     * 接收消息
     *
     * @param eventType
     * @param <T>
     * @return
     */
    public <T> Observable<T> toObserverable(Class<T> eventType)
    {
        return bus.ofType(eventType);
    }
}

最后在使用的时候调用:

 RxBus.getInstance().post(new Event());

二.容器式

原理: 创建一个单例容器,里面有Activity栈Stack stack;并创建add,remove,finishAll 方法

在 BaseActivty onCreat 的时候执行add,onDestroy 执行remove ;在我们需要的时候执行finishAll 方法

三.广播式

原理:在BaseActivity中代码注册广播接受者,需要的时候发送一个广播

public class BaseActivity2 extends AppCompatActivity {
    private static final String EXITACTION = "action.exit";
    private ExitReceiver exitReceiver = new ExitReceiver();
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        IntentFilter filter = new IntentFilter(); 
        filter.addAction(EXITACTION);
        registerReceiver(exitReceiver, filter); 
    }
    @Override protected void onDestroy() { 
        super.onDestroy(); unregisterReceiver(exitReceiver);
    }
    class ExitReceiver extends BroadcastReceiver { 
        @Override public void onReceive(Context context, Intent intent) {
            BaseActivity2.this.finish(); 
        } 
    }

}

SingleTask 方法

原理:将MainActivity 设置成singleTask,根据Activity的启动特性,singTask 的MainActivity只要一出现,就没有其他的Activity存在了,所以在MainActivity退出即可

SingleTask 改进版

依然设置MainActivity的启动方式

android:launchMode="singleTask"

重写onNewIntent()

@Override
    protected void onNewIntent(Intent intent) { 
        super.onNewIntent(intent); 
        if (intent != null) { 
            boolean isExit = intent.getBooleanExtra(TAG_EXIT, false); 
            if (isExit) { this.finish();
            }
        } 
    }
}

不管以什么方式启动的MainActivity ,都会调用onNewIntent 方法,所以我们使用

Intent intent = new Intent(this,MainActivity.class); intent.putExtra(MainActivity.TAG_EXIT, true);
 startActivity(intent);

的时候就会退出app;

猜你喜欢

转载自blog.csdn.net/qianyedoufulao/article/details/80497696
今日推荐