How to finish () pressing the "Activity at home" button in android?

I want to finish the activity button on the home button. I have the code below, but its click event fails. but still don't get clickevent

public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_HOME: System.out.println("Home clicked...."); return true; } } return super.onKeyDown(keyCode, event); } 
+6
source share
8 answers

When the Home button is pressed, the onStop method is called in your activity. So you can add finish(); to the onStop method to destroy your activity. In the end, the onDestroy method will be raised to confirm that your activity is complete.

+13
source

You cannot process the Home button.

You can try to override some of these methods: onStop , onUserLeaveHint

Are you sure you need to do this?
If you can save your current state through onPause / onSaveInstanceState and restore it later, the OS should be able to process the Activity life cycle and complete it if necessary. Can you tell us what is stopping you from doing this? :)

+6
source
 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); moveTaskToBack(false); return true; } return super.onKeyDown(keyCode, event); } 
+2
source

end your activity on onUserLeaveHint() without this method when the user presses the home key button, so just redefine this method and end the call () in this method.

+1
source

You can put the finish () in onPauseMethod in such a way as soon as the action of the home button ceases to be the main active current activity and enters the onPause method, this will stop this activity.

0
source

Shut down when the Home button is pressed. Her work is perfect for me.

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_HOME)) { Log.v("hari", "KEYCODE_HOME"); onUserLeaveHint(); return true; } if ((keyCode == KeyEvent.KEYCODE_BACK)) { return true; } if ((keyCode == KeyEvent.KEYCODE_MENU)) { return true; } return false; } public void onUserLeaveHint() { // this only executes when Home is selected. super.onUserLeaveHint(); if (!isFinishing()) { Log.v("hari", "if condition working"); finish(); } } 

but sometimes the back button does not work when using this code ...

0
source

You can finish the action when the action is brought to the foreground from the icon on the main screen / desktop. See here: https://groups.google.com/forum/#!topic/android-developers/D9CyrSzIm5c

Basically, he says to add android: clearTaskOnLaunch = "true" to your activity.

0
source

see this

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_HOME: finish(); System.out.println("Home clicked...."); return true; } } return super.onKeyDown(keyCode, event); } 
-2
source

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


All Articles