How can I close the application by clicking on android

I want to go to the main screen when I press the back button. I am using this code.

public void onBackPressed() { this.finish(); return; } 
+4
source share
12 answers

Pressing the BACK button will effectively call finish() for you. No need to grab the BACK key.

I assume that your problem is that when you press the BACK key, it just goes back to the previous Activity in your application.

If so, make all actions "self-complete" when they start a new Activity in your application ....

 startActivity(new Intent(this, MyNewActivity.class)); finish(); 

If you do this, there will be no Activity to return when you press BACK, and it will always return to the main screen.

+18
source

You can try this

 @Override public void onBackPressed() { Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); } 
+6
source

Ok, to run the Home sceen of your device use this code in onKeyDown()

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // do something on back. Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); this.finish(); return true; } return super.onKeyDown(keyCode, event); } 

And if you want to close your application, I think that either you must close all the finish() actions in a way (in some standard way), or using

android.os.Process.killProcess(android.os.Process.myPid()) this code kills your application. (Some kind of ugly way !!)

+3
source

You must override the finish ()

 @Override public void finish() { System.out.println("finish activity"); SaveData(); System.runFinalizersOnExit(true) ; super.finish(); android.os.Process.killProcess(android.os.Process.myPid()); } 

Then call this method as follows:

 @Override public void onBackPressed() { this.finish(); } 
+3
source

When you call NewAcitivy to call finish() . After startActivity , so that the previous activity startActivity .

Then use

 @Override public void onBackPressed() { super.onBackPressed(); this.finish(); } 
+2
source

You can try the following:

 public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ this.finish (); } return true; } 
+1
source

u want to override OnkeyDown and shut down

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // do something on back. this.finish(); return true; } return super.onKeyDown(keyCode, event); } 
0
source

The easy way is to just end your current activity when you move on to the next action.

Just do finish() after startActivity() , then click on the back button from any activity that you can just call finish() to exit the application, since all other actions are not on the stack.

0
source

You can close the application from anywhere to trigger this type of intent:

 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 

This is the best way to close the application from anywhere, to any click event.

0
source

You need to start by using a flag to clear actions on top of the actual activity.

 public void onBackPressed() { Intent intent = new Intent(this, HomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); this.startActivity(intent); this.finish(); } 
0
source

Quote from the Official Guide

FLAG_ACTIVITY_CLEAR_TOP

If it is established and the launched activity has already been launched in the current task, then instead of launching a new instance of this activity, all other activities on top of it will be closed and this intention will be transferred (now from above) to the old activity as a new intention.

FLAG_ACTIVITY_CLEAR_TASK

If set in the Intent passed to Context.startActivity (), this flag will trigger any existing task that will be associated with before starting work. That is, the activity becomes the new root of another empty task, and any old activities are completed. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

FLAG_ACTIVITY_NO_HISTORY

If set, the new activity is not saved in the history stack. As soon as the user moves from him, the action is completed. It may also need to be set with the noHistory attribute.

FLAG_ACTIVITY_NEW_TASK

If set, this operation will be the start of a new task on this story stack.

 @Override public void onBackPressed() { Intent intCloseApp = new Intent(Intent.ACTION_MAIN); intCloseApp.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intCloseApp.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intCloseApp.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intCloseApp.addCategory(Intent.CATEGORY_HOME); intCloseApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intCloseApp); } 
0
source
 public void onBackPressed() { System.exit(1); return; } 

Brutal way

0
source

Source: https://habr.com/ru/post/1390171/


All Articles