问题描述
我有2个活动,所以活动一进入活动2然后在活动2我有一个退出按钮。但是,当我点击它,它都只能退出了活动数量2,再次回到活动1。它基本上感觉就像我刚刚重新启动的应用程序。我不知道为什么?
这是我的code。
按钮btExit =(按钮)findViewById(R.id.btExit);
btExit.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
完();
System.exit(0);
}
});
System.exit(0);
是终止Android应用的一个不错的方法。 Android的管理它自己的操作系统
您可以通过其相应的意图打开主页的应用程序:
意向意图=新的意图(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意向);
希望这有助于
编辑: -
那么我想你的目标是在完成所有堆积起来的活动。
这是 -
关闭所有的previous活动如下:
意向意图=新的意图(这一点,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(我退出,真正的);
startActivity(意向);
完();
然后在 MainActivity 的onCreate()
方法添加此完成MainActivity
如果(getIntent()。getBooleanExtra(我退出,FALSE)){
完();
}
其结果将与上面相同,但因为你所有的堆叠已经关闭,当你回来给你的应用程序就必须从你的主要活动,即发射活动的开始。
希望这有助于。
I have 2 activity, so activity 1 go to activity 2 then on activity 2 I have an exit button. But when I click it, all it only exited the activity number 2 and return to activity 1 again. Its basically felt like I just started the application again. I am not sure why?
This is my code.
Button btExit = (Button) findViewById(R.id.btExit);
btExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
System.exit(0);
is a bad way of termination of android apps. Android manages it in its own os
You can bring up the Home application by its corresponding Intent:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
hope this helps
EDIT :-
Then I suppose you are aiming at finishing all the stacked up activity..
Here it is :-
Closing all the previous activities as follows:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();
Then in MainActivity onCreate()
method add this to finish the MainActivity
if( getIntent().getBooleanExtra("Exit me", false)){
finish();
}
The result will be same as above, but because all your stacked up activities are closed, when you come back to you app it must start from your main activity i.e launcher activity.
Hope this helps.
这篇关于System.exit(0)犯规关闭所有我的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!