How can I exit from my app?

前端 未结 2 1254
暖寄归人
暖寄归人 2021-01-26 04:42

How can I exit from my app?

I want that when the user will clicked on the button, the app will exit completely, I saw any answers about it, but they took the app to back

2条回答
  •  悲哀的现实
    2021-01-26 05:06

    in this method you have can call exit() function anywhere and get exit from the app. so For closing an app at any point use FLAG_ACTIVITY_CLEAR_TOP flag in Intent & then system.exit();

    NOTE : I think in way it will not break the Android Life cycle

    In a place where you need to execute EXIT from the app

    public void exit() {
        startActivity(new Intent(this, HomeActivity.class).
        setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK).putExtra(EXIT_FLAG, true));
    }
    

    MainActivity.onCreate()

    protected void onCreate(Bundle savedInstanceState) {
        if (getIntent().getBooleanExtra(EXIT_FLAG, false)) {
            if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
                finish();
        }
    }
    

提交回复
热议问题