Вопрос

How do I exit my app programmatically from within my app? I have an exit button that brings up a dialog prompting the user to confirm exit. The back button does the same thing. This works usually. However, I'm also launching intents back to my MainActivity when I receive certain events, and the user taps on the notifications. I notice that when the intent is launched in this case, and then the user tries to exit, the app does not exit, but rather seems to go back to the previous instance. If I tap on 3 notifications which launches 3 intents back to MainActivity, I realize that I need to exit the app 3 times before it goes back to the homescreen.

Below is my exit code:

private AlertDialog alert;
if (alert != null) {
  if (alert.isShowing()) {
   alert.dismiss();
  }
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Exit");
builder.setMessage("Are you sure you want to exit?").setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
    getActivity().finish();
    System.exit(0);
  }
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();
  }
});
alert = builder.create();
alert.show(); 

Here is my notification/intent code

Intent launchIntent;
launchIntent = new Intent(context, MainActivity.class);
launchIntent.putExtra("myType", type);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder noti = new NotificationCompat.Builder(
  context).setContentTitle(context.getResources().getString(R.string.app_name))
  .setContentText(notificationMessage)
  .setSmallIcon(R.drawable.ic_stat_notify)
  .setContentIntent(pendingIntent)
  .setAutoCancel(true)
  .setWhen(System.currentTimeMillis())
  .setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify(notifyId, noti.build());
Это было полезно?

Решение

First two consideration:

  • Don't use an exit button in your application, and more important don't bother the user with a dialog when he presses back, asking if he want to exit. The android system will take care to shut down your app when the user exit with the back key.
  • You don't need to call System.exit(0) to terminate an Activity, just Activity.finish()

To avoid an Intent to call multiple instance of the same Activity remove the FLAG_ACTIVITY_NEW_TASK flag from the Intent and add the FLAG_ACTIVITY_CLEAR_TOP one.

From Doc:

FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

FLAG_ACTIVITY_NEW_TASK
If set, this activity will become the start of a new task on this history stack.

Другие советы

You are starting a new Instance of your Activity via using the FLAG_ACTIVITY_NEW_TASK. You shouldn't be doing this. You should use your old activity otherwise you keep creating new instances. What is hapepning is each task that you are creating is being added to the ActivityStack and when you hit back it traverses through this list. If you created three instances then it will show your activity 3 times each time you hit back.

You can make your activity as Single instance so that from anywhere you call the device will return the same activity from the stack.

In Manifest.xml

" ... android:launchMode="singleInstance">

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top