问题
Hi i want to know how to close an application in Android. Actually i am having idea by using the finish() method we can close present activity.. But what i want is, the following code defines remaining...
Main.java
Handler handle=new Handler();
handle.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(ZzzzzProjActivity.this,Second.class));
}
}, 3000);
Second.java
/*** same above code***/
Third.java
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
Toast.makeText(getApplicationContext(), "backbutton", 30).show();
finish();
}
return super.onKeyDown(keyCode, event);
}
As per the following code after coming to Third.java, when i click back button it is navigating back to Second.java page. But what i want is my applications needs to close totally. Can anyone help me with this....
回答1:
for Achive that you should finish the current Activity when you call another Activity from Current one, like when you call second.class from first one you should finish first one and so on...,
i.e when you call another activity.class then you should finish the currentActivity.class by
like
CurrentActivity.this.finish();
回答2:
you can create a dialog activity asking if the user wants to exit. in the intent you can set
Intent.FLAG_ACTIVITY_CLEAR_TOP
It will clear everything the stack and close you app. or you can do what @user1208720 has suggested.
回答3:
Use that code to finish your application
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
回答4:
Finish each activity as you are leaving it. Like this whenever you click back, there are no previous activities so the application will exit.
Another way (that is usually not recommended for Android) is to use "System.exit(0);"
来源:https://stackoverflow.com/questions/10780887/closing-an-android-application