3

I have an application where I've 5 Activity. One Menu Activity and another 4 child Activity attached to menu screen. So i can select any Activity and then cane back to menu. Suppose I move around like this

Menu -> Activity1 -> Menu -> Activity3 -> Activity2 -> Menu

and now I press Home/Back Button and I want that my app will show me a alert dialog for exit confirmation , If I press yes then kill all Activity and show the home screen.

I've tried those finish() , System.exit(0) but no luck there . finish() and System.exit(0) both finishes the current activity not all of them. Please Show me a way.

Amitabha
  • 1,664
  • 1
  • 12
  • 30

4 Answers4

0

I think it is better to stop child activity when in background. For example

Menu > Activity1 > 

Now if you goes to menu again then finish Acitivy 1.

Again Menu > Activity 3 > Activity 2 > Menu

when you goes to Activity 3 then Activity 2.. then start Activity 2 from 3 by using start activity for result. SO if you want to go to menu from activity 2. then you can go through Activity 3..

Another better solution for this to use CLEAR_TOP , SINGLE_TASKS flag to open menu. So when you goes to menu only menu activity is active. Then by calling finish you can finish your application.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

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 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.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • yes it works but i would not recommend that check the link and comments by warrenfaith. Its a bad design http://stackoverflow.com/questions/16691773/android-onbackpressed-not-working-for-me/16692532#16692532 – Raghunandan May 23 '13 at 09:11
  • @Raghunandan, yup I agree with you. Reason I mention it is dirty. But working like a charm in my application. – Lazy Ninja May 23 '13 at 09:12
  • i too suggested the same as mentioned in the link it was voted down. so i suggest you edit your answer. warren faith suggested action bar for the same – Raghunandan May 23 '13 at 09:14
0

Yes, you can. There are 2 different aproches

  1. 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();
    
  2. Explicity declare that activities skip stack:

    < activity android:name=".Activity1" android:noHistory="true" ... />
    < activity android:name=".Activity2" android:noHistory="true" ... />
    
Igor S.
  • 3,332
  • 1
  • 25
  • 33
0

Finish the Activity, when home button Clicked.put this code in your activity. Its Working 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 back button not working when use this code...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
harikrishnan
  • 1,985
  • 4
  • 32
  • 63