3

Possible Duplicate:
Quitting an application - is that frowned upon?

I've written an android app, that has 5 activities. Each activity is started by the activity before it. When the user gets to the last activity, i have added an "EXIT" button. When this button is pressed, i call finish(); However this only closes the current activity, and the app returns to the previous activity. Is there an easy way to terminate all activities when exit button is pressed.

thanks

Community
  • 1
  • 1
Coops5575
  • 431
  • 2
  • 5
  • 16
  • 1
    This is a duplicate. Please read Mark's excellent [answer](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238). – Matthew Mar 09 '11 at 16:54

4 Answers4

5

If you don't care about the last opened activity, then call finish on each activity just after you call startActivity calling the next one.

If you do care, and just want to kill'em all when you click the Exit button on the last activity, then use BroadcastReceivers:

  • Create a BaseActivity like this:

public class BaseActivity extends Activity {
    private KillReceiver mKillReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mKillReceiver = new KillReceiver();
        registerReceiver(mKillReceiver,
            IntentFilter.create("kill", "spartan!!!"));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mKillReceiver);
    }
    private final class KillReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }
}

  • Make your activities extend that BaseActivity.
  • Whenever you want to clear the stack:

Intent intent = new Intent("kill");
intent.setType("spartan!!!");
sendBroadcast(intent);

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Hey cristian, what is this GuiceActivity class? kind of robotguice? – papachan Mar 15 '11 at 16:29
  • Ups... that's something of one of my project. Yes, it has to do with roboguice, but it's not relevant for this case. Let me edit my question... thanks for the shot. – Cristian Mar 15 '11 at 16:31
5

Usually it is not a good idea to add "exit" feature to an app. That's not in Android nature. Read the topic first.

Community
  • 1
  • 1
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
2

catch this result in onActivityResult() and then just propagate it through the stack?

Ian
  • 3,500
  • 1
  • 24
  • 25
  • 1
    This doesn't make sense to me, not all activities return a result and what about cases where you are coming back from activities outside of your application stack? – Andrew White Mar 09 '11 at 16:23
1

Do not add an exit button. It almost never makes sense in the Android world. Read this article by a Google employee for some valuable insight why.

Andrew White
  • 52,720
  • 19
  • 113
  • 137