2

I have an Android App, and has several activities. I need to provide the button for each activity or specific activity which should allow to Close the App, without going to back to previous activities or run in background. I tried

finish();
System.exit(0);

Both combination and individually its not working but closing the current activity and navigate to previous activity.

I looked the code from the following question

Need code for exit from app in android

Community
  • 1
  • 1
AjayR
  • 4,169
  • 4
  • 44
  • 78
  • http://stackoverflow.com/a/11815000/3419997 – turtle Jul 30 '14 at 08:06
  • You have to just finish the current activity when You go to the next activity, then the backbutton finish() will work..and dont use System.exit(0)....please – Opiatefuchs Jul 30 '14 at 08:12
  • http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html regarding a "real" exit button. The rest was already said above – Droidman Jul 30 '14 at 08:16
  • Possibly duplicate of : http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon or http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically – VVB Jul 30 '14 at 08:17
  • You should check this http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – Aniruddha Jul 30 '14 at 08:18
  • @Anirudhha I already posted that link – VVB Jul 30 '14 at 08:19

7 Answers7

1

First, having a Quit button is not reccomended in Android as it's not part of the expected UX.

But if you really need it, then you can call your home activity with an intent containing an extra, for instance :

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("FORCE_EXIT", true);

Finally in your home activity, when handling the intent (in the onNewIntent() method), if the "ForceExit" extra is set, then finish the activity.

The stack will have been cleared completely by the FLAG_ACTIVITY_CLEAR_TOP and your app will then stop.

XGouchet
  • 10,002
  • 10
  • 48
  • 83
  • Intent.FLAG_ACTIVITY_CLEAR_TOP is not working, again its moving to previous intent. Doing anything wrong – AjayR Jul 31 '14 at 07:06
1

The most recommended approach that works for most cases is to feature only 1 Activity, using fragments for content displaying and logic.

This way you only need to finish() the main Activity since it will control the app lifecycle by design. You will have many other benefits, such as dependency control and reusability, aswell as built-in functionality like animations using fragment transactions while having the possibility of keeping a fragment backstack, which you can manage accordingly towards your expected user interaction and without affecting the conveniency of finishing your app by calling finish() on your host Activity.

Another thing you can do, is to flag intents like this: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); before launching a new Activity.

This way you can maintain your back trace clean, hence finishing the application whenever the user press the back button or call finish() from any event. However the use of flags is discouraged and considered bad practise.

SuppressWarnings
  • 4,134
  • 4
  • 25
  • 33
1

This may be a hack to solve your problem. but i have just made an app and tested my code and it is working fine.

You will need to create a new activity called QuitActivity or whatever you want to name it and when you want to finish your app or quit your app you will have to start that activity via using this code

    Intent i = new Intent(getApplicationContext(), QuitActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    this.finish();

then this is my code for quit activity that does nothing but closes it self after clearing the backstack so your app will quit.

public class QuitActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.finish();
}

}

hope this helps you.

Umer Kiani
  • 3,783
  • 5
  • 36
  • 63
0

Use:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

when starting a new Activity, as described in API Documentation.

arghtype
  • 4,376
  • 11
  • 45
  • 60
Ian
  • 2,024
  • 2
  • 12
  • 12
0

First Finish your current Activity while moving to next by this code

startActivity(intent);
Classname.this.finish();

Second thing just override onBackPressed

@Override
        public void onBackPressed() {
            //do nothing
        }
Meghna
  • 539
  • 4
  • 14
0

type only System.exit(0); or create static boolean needToExit; set needToExit = true;

in place where you call

finish();
System.exit(0);

in all other activity overwrite onresume

public void onResume(){
super.onResume();
if (MySettingsClass.needToExit){
finish();
System.exit(0);
return;
}

//your other code;
}
QArea
  • 4,955
  • 1
  • 12
  • 22
0

I tried some of the answers here using the flags or System.exit(0), but for me it either didn't work or it resulted in weird behavior. Sometimes it would kill the app, but then immediately restart it. I realized that I should just be doing things more of the standard way using request and result codes.

Basically, in your parent activity, start your child activity with:

startActivityForResult(new Intent(this, ChildActivity.class), CHILD_ACTIVITY_REQUEST_CODE);

where CHILD_ACTIVITY_REQUEST_CODE is just a constant (static final) integer. Then in your child activity, when they press the exit button, finish the activity with:

setResultAndFinish(RESULT_CODE_EXIT);

where RESULT_CODE_EXIT is another constant (static final) integer. Then back in your parent activity, handle the result code and finish the parent activity too:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CHILD_ACTIVITY_REQUEST_CODE) {
        if(resultCode != ChildActivity.RESULT_CODE_EXIT) {
            finish();
            return;
        }
   }
}
Mike Hall
  • 1,151
  • 2
  • 10
  • 24