怎样用代码执行退出终止App

岁酱吖の 提交于 2020-03-05 15:23:18

http://stackoverflow.com/questions/23703778/exit-android-application-programmatically

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project

发送intent到开启App时启动的第一个Activity,设置Intent.FLAG_ACTIVITY_CLEAR_TOP所以除了第一个Activity其他的都被关闭了。


Intent intent = new Intent(getApplicationContext(),FirstActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 intent.putExtra("EXIT", true);
 startActivity(intent);



The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate

然后关闭第一个Activity。

if (getIntent().getBooleanExtra("EXIT", false)) { finish(); }

and you are done....


后续:

  对这个方法,我还有一些疑惑,再测试几遍后再来补充。




标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!