简体   繁体   English

如何以编程方式关闭并重新启动应用程序?

[英]How can i close and start again app programmatically?

In my application I want close my app and start again it programically and for this I write below codes. 在我的应用程序中,我想关闭我的应用程序并以编程方式再次启动它,为此,我在下面编写了代码。
But in my code just close my app and not start again it? 但是在我的代码中,只是关闭我的应用程序而不重新启动它吗?

My code: 我的代码:

private void exitFromApp() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

How can I fix this issue? 如何解决此问题?

Try this instead of Closing the Application 试试这个,而不是关闭应用程序

Intent intent = new Intent(this, YOUR_STARTACTIVITY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

Or 要么

Intent intent = new Intent(this, YOUR_STARTACTIVITY.class);
startActivity(intent);
finishAffinity();
finish();

The above two method can be used to start Your base activity by removing all background activities 通过删除所有后台活动,可以使用以上两种方法来开始您的基础活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM