0

Is there any way to close android app ? I cannot use finish because it shows previous activity and I have lot off activities behind ( for example from 1st to 2nd .. to 10th and on 10th I want to exit from app ). How to achieve this ?

Damir
  • 54,277
  • 94
  • 246
  • 365
  • There is a similar type of question on SO. This might help you. http://stackoverflow.com/questions/2092951/how-to-close-android-application – Sadat Nov 24 '11 at 09:12

5 Answers5

1

I also had the same question and did not find a way to definitelly KILL an app. But later I discovered it is due to the intrinsic nature which the system was designed of.

If you want to close an app, you just have to clear the Activity stack and free the usage resources. It seems strange, right? You should have the power to close your own app. But it is really not recommended on Android apps and it does not follow the Android guidelines. And also, do not provide exit options to users. For Android applications which needs to provide an "exit option" to user (for example, due to security reasons), we should provide a "logout option", clear the Activity stack, free resources and other stuff you want to do, but never kill the Main Activity.

The solutions to clear the Activity stack have been shown for other answers here: (1) to use Intent with flag FLAG_ACTIVITY_CLEAR_TOP or (2) to use Broadcast.

With FLAG_ACTIVITY_CLEAR_TOP, you put an Intent on the current Activity:

Intent intent = new Intent(getApplicationContext(), YourMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

And the option with Broadcast, is to send a broadcast from the current Activity to all Activities you want to close.

For this, you have to SEND the broadcast (on the current Activity):

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_SPECIFICNAME");
sendBroadcast(broadcastIntent);
finish();

And RECEIVE this broadcast on the others Activities (on each one you want to close):

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_SPECIFICNAME");
registerReceiver(new BroadcastReceiver() {
@Override
   public void onReceive(Context context, Intent intent) {
   Log.d("onReceive","Logout in progress");
            finish();
   }
}, intentFilter);

And that is it! I hope this answer helps you. Sorry for my english. I know that it is not the answer you were waiting for, but I think that is the current (and the "correct") solution.

Sarah Sakamoto
  • 939
  • 1
  • 10
  • 13
1
Intent intent1 = new Intent(context, Activity.class);
             intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
             startActivity(intent1);

CLEAR TOP clear your stack of activity and can finish the last activity you redirect

if your activity stack is proper clear top will work for you

OPTION 2:

using Borad cast receiver chk link

ud_an
  • 4,939
  • 4
  • 27
  • 43
1

How about using android:noHistory

Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false". A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

Reno
  • 33,594
  • 11
  • 89
  • 102
0

You can use (but it's not recommended)

System.exit(0);

or you can move your app in background (that is the way Android manages multitasking)

moveTaskToBack(true);

Mangusto
  • 1,505
  • 1
  • 13
  • 29
0

Place this code on exit button press

Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

And override the onResume of initial activity with a finish();

xydev
  • 3,409
  • 5
  • 34
  • 54