How to quit android application programmatically

前端 未结 30 2088
迷失自我
迷失自我 2020-11-22 03:06

I Found some codes for quit an Android application programatically. By calling any one of the following code in onDestroy() will it quit application entirely?

30条回答
  •  花落未央
    2020-11-22 03:21

    The easiest way I found to quit an application from an activity, without breaking Android's logic and without adding more code in existing activities and passing extras is the following:

    public static void quitApplication (Activity currentActivity) {
        Intent intent = new Intent (currentActivity, QuitApplicationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        currentActivity.startActivity (intent);
        currentActivity.finish ();
    }
    

    the QuitApplicationActivity being:

    public class QuitApplicationActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
    
            finish ();
        }
    }
    

提交回复
热议问题