0

I want to kill my app from a exit button on my navigation drawer. the drawer has a exit button that is suppose to just kill the whole app (all activity) and gets the user out of the app .

When i am using finish() in the first activity its working but when i go into the app and call finish() the activity gets killed and returns to previous activity but never really kills the app.

Tried to use System.exit(0) but no luck.

Extra Information Might Help

I have all my activity started with android:launchMode="singleTop" . it means all those activities that are already created will not be created again and just reordered to front on calling.

Do anyone have any suggestion for this , please do help.

Update

I want to make some updates here as my question looks like this SO Question.

As i have already said i am using a android:launchMode="singleTop" . and this answer is not working for my case. I have to call onCreate() to make this happen but it is not my case.

Community
  • 1
  • 1
Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52

5 Answers5

2

Use below code

public void AppExit()
    {
        this.finish();
        Intent intent= new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);
    }

Call above function to exit from App.

Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
1

First, you need to decide if you just need to take the user out of the app, or if you also need to finish() all of your activities. It depends on your needs, but my guess is that in most cases most users won't know the difference, and Android is designed to have many apps having activities still running. If this is all you need, then you can just use an Intent to take the user to the home screen.

Intent intent = new Intent(Intent.
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);

If you actually want to finish() your activities, then you need a mechanism to inform all currently running activities that the user is exiting the app. You can use something like EventBus to send an event that will be received by all registered subscribers; if you don't want to use a third-party library, you can do the same type of thing with LocalBroadcastManager. To do that, you would register a BroadcastReceiver with an IntentFilter for a custom action and broadcast an Intent with that action when the user wants to exit.

In either case, every activity should receive the signal and call finish() on itself. Note that the subscription (in the EventBus case) or the registration of a receiver (in the LocalBroadcastManager case) must be still active after onStop(), so I would register in onCreate() and unregister in onDestroy().

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

Use below code in your exit button

Intent intent = new Intent(this, ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

And, in your ActivityOne.class oncreate() method just put below code

if (getIntent().getBooleanExtra("EXIT", false)) 
{
    finish();
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Masum
  • 4,879
  • 2
  • 23
  • 28
0

First of all, Android apps are not intended to be killed other that by the system, which attempts to keep them in memory in case the user will return.

But, if you have to do it, try this:

android.os.Process.killProcess(android.os.Process.myPid());
dev.bmax
  • 8,998
  • 3
  • 30
  • 41
0

As a few answers say, call finish(); after you start an intent from an activity. That will make sure the current activity is the only running activity. Also try super.finish(), which will call finish() at parent activity.

You can alternatively use only one activity with many fragments. That way, if you call finish() in the exit button OnClickListener code, you'll exit the app. This will also save a lot of coding since you will be defining the navigation drawer once.

Rishabh Tatiraju
  • 172
  • 1
  • 10