Mitt
Mitt

Reputation: 91

Android:Unable to exit by a single click

i am going from Activity A to Activity B, and reverse, In Activity A, i have Exit Button, when i click exit it doesn't exit, i have to press two times to exit from application when i return from Activity B to Activity A. please help

Upvotes: 0

Views: 234

Answers (2)

Rich
Rich

Reputation: 36806

This means that there are multiple instances of your activity in memory. If you want to close all of them, you can register a broadcast receiver in onResume, and this broadcast receiver will call finish() on the activity. Your exit button then sends the broadcast intent that this receiver receives

something like the following...

defined in your activity:

BroadcastReceiver activityKillerOnTheLoose = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        finish();
    }
};

in your onResume method (register to receive broadcasts)

@Override
public void onResume()
{
    super.onResume();

    registerReceiver(
            activityKillerOnTheLoose,
            new IntentFilter(ActivityKillerBroadcastKey));

in your onPause (make sure to unregister!)

@Override
public void onPause()
{
    super.onPause();
    unregisterReceiver(activityKillerOnTheLoose);

and to broadcast...

Intent activityKillerBroadcast = new Intent(ActivityKillerBroadcastKey);
sendBroadcast(activityKillerBroadcast);

Upvotes: 1

James Black
James Black

Reputation: 41858

What function are you calling to exit?

With Android, if you go from one Activity to another these are pushed onto a stack, so when you use the back button, for example, it just pops the activity now on the top.

For some discussion as to how you may want to handle this you can look at this question: How to dismiss previous activities in android at a certain point?

Or, this may be a better question: Closing several android activities simultaneously

Upvotes: 1

Related Questions