Android: always run top activity when clicking on notification

EDIT : note: I completely repeated my question.

I have an application with two Activites : A and B. Activity A - MAIN . So, the application starts, and A appears on the screen. The user presses a button on it, and a new Activity B appears on the screen.

So, now in my "back stack" there are 2 actions: A and B.

Now I press the Home key and then click the icon of my application on the launch bar: Activity B appears on the screen ( not A ) as much as possible since this is a lot of activity in my task.

Now the question is: how can I do Intent to open the current top Activity my task?

I need to use it in Notification : when the user clicks on my Notification , the top Activity this task should appear on the screen, not the specified one.

I have tried many Intent flags, such as SINGLE_TOP and others, but I still can not get what I need.

Does anyone know a solution?

0
android android-activity notifications
source share
3 answers

Well, after spending several hours in a few days, I found a solution:

 Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 

This is a way to launch applications, and it works great for Intent notifications.

+7
source share

I used this.

 private void notifyUser(Context context) { notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); CharSequence text = "Test Notification"; Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis()); Intent resultIntent = new Intent(context, StartActivity.class); resultIntent.setAction(Intent.ACTION_MAIN); resultIntent.addCategory(Intent.CATEGORY_LAUNCHER); resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(GeneralSettingsActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); notification.flags = Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(context, "Title", text, resultPendingIntent); notificationManager.notify(AUTOSYNC_COMPLETED_NOTIFICATION, notification); } 
+1
source share

you can use android: launchMode = "singleInstance". Let it work.

0
source share

All Articles