How to quit android application programmatically

前端 未结 30 2047
迷失自我
迷失自我 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:22

    Create a ExitActivity and declare it in manifest. And call ExitActivity.exit(context) for exiting app.

    public class ExitActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            finish();
        }
    
        public static void exit(Context context) {
            Intent intent = new Intent(context, ExitActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        }
    
    }
    

提交回复
热议问题