Three more elegant ways to exit the program in Android

 
 

1. RS elegant

What is the RS style? Namely Receiver+singleTask. We know that Activity has four loading modes, and singleTask is one of them. After using this mode, when starting Activity, it will first query whether there is an instance of Activity in the current stack. If it exists, it will be placed on the top of the stack. And remove all activities above it from the stack. We open an app, first a splash page, and then finish the splash page. Jump to the home page. Then there will be N jumps on the home page, during which an indeterminate number of activities will be generated, some of which are destroyed, and some reside in the stack, but the bottom of the stack is always our HomeActivity. This makes the problem a lot simpler. We can gracefully exit the app in just two steps.

  • 1. Register an exit broadcast in HomeActivity, which is the same as the second broadcast, but here you only need to register on a page of HomeActivity.
  • 2. Set the startup mode of HomeActivity to singleTask.

When we need to exit, we only need startActivity(this, HomeActivity, class), and then send an exit broadcast. The above code will first remove all activities above HomeActivity in the stack and pop it, and then receive the broadcast to finish itself. Everything is OK! There is no pop-up box, no need to consider the model Rom adaptation. There will be no memory problems, it's just so elegant and simple!

2. SingleTask redesign

After talking with some guys, many of them said that registering for broadcasting is a little troublesome, and the guys downstairs proposed a simpler way, the idea is also very simple,

  • 1. Set the loading mode of MainActivity to singleTask
  • 2. Rewrite the onNewIntent method in MainActivity
  • 3. Add the exit tag in the Intent when you need to exit

Since many small partners are eager to source code, we will explain this method directly in the form of code.

The first step is to set the loading mode of MainActivity to singleTask

 android:launchMode="singleTask"

第二步重写onNewIntent()方法

    private static final String TAG_EXIT = "exit";

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

第三步 退出

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

3、懒人式

这种方式更加简单,只需要如下两步操作

  • 1、将MainActivity设置为singleTask
  • 2、将退出出口放置在MainActivity

我们可以看到很多应用都是双击两次home键退出应用,就是基于这样的方式来实现的,这里在贴一下如何处理连续两次点击退出的源码

private boolean mIsExit;
@Override
    /**
     * 双击返回键退出
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mIsExit) {
                this.finish();

            } else {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                mIsExit = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mIsExit = false;
                    }
                }, 2000);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770308&siteId=291194637