How to close all actions by clicking the "Hardware Home" button?

I have an application in which I have 5 Activity. One action in the menu and 4 more child actions connected to the menu screen. Therefore, I can select any activity, and then return to the menu. Suppose I move like this.

Menu → Activity1 → Menu → Activity3 → Activity2 → Menu

and now I press the Home / Back button, and I want my application to show me a confirmation dialog to confirm the exit. If I click "yes", then kill all the activity and show the initial screen.

I tried those finish (), System.exit (0), but no luck. finish () and System.exit (0) do not complete the current activity with all of them. Please show me the way.

+3
source share
4 answers

I think it’s better to stop the child’s activity when in the background. for instance

Menu > Activity1 > 

Now, if you go to menu again, then complete Acitivy 1 .

Again Menu > Activity 3 > Activity 2 > Menu

when you go to Activity 3, then Activity 2 .. then run Activity 2 of 3, using the initial activity for the result. SO, if you want to go to the menu from step 2. then you can go through Activity 3.

Another best solution is to use CLEAR_TOP , SINGLE_TASKS to open the menu. Therefore, when you go to the menu, only the activity of the menu is active. Then, causing termination, you can end your application.

0
source

This worked for me:

  Intent intent = new Intent(this, TopMenuActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(Utils.INTENT_EXIT, true); startActivity(intent); finish(); // finish the current activity 

And then in the TopMenuActivity onCreate () method add the following

  if( getIntent().getBooleanExtra(Utils.INTENT_EXIT, false)){ finish(); // finish TopMenu and go HOME return; // prevent from doing unnecessary stuff below } 

Dirty yes! But it works.

0
source

Yes, you can. There are 2 different aproches

  • Use onActivityResult

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request we're responding to if (resultCode == QUIT_ALL) { // setResult(QUIT_ALL,returnIntent); finish(); } } //last activity setResult(QUIT_ALL,returnIntent); finish(); 
  • The explanation declares that actions skip the stack:

     < activity android:name=".Activity1" android:noHistory="true" ... /> < activity android:name=".Activity2" android:noHistory="true" ... /> 
0
source

Complete the action when the Home button is clicked. Enter this code in your activity. His 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

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


All Articles