Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should i do from notification back to activity without new intent

Tags:

from Android Development, i implement a simple notification by the sample code, but in my app, i don't want to new intent and create the activity again, i just want to back to my last activity(it's a mediaplayer UI). if i use the sample code, it will create a new activity by

Intent resultIntent = new Intent(this, ResultActivity.class); 

i comment relative code about new intent and got a notification, but didn't have idea how to back to my last activity...

back to my app from long press home key and touch my app is OK. what i want is just like this behavior.

bleow is the sample code from Android Development, i comment the new intent portion.

NotificationCompat.Builder mBuilder =     new NotificationCompat.Builder(this)     .setSmallIcon(R.drawable.notification_icon)     .setContentTitle("My notification")     .setContentText("Hello World!"); /*Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(ResultActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent =     stackBuilder.getPendingIntent(         0,         PendingIntent.FLAG_UPDATE_CURRENT     ); mBuilder.setContentIntent(resultPendingIntent);*/ NotificationManager mNotificationManager =     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(mId, mBuilder.build()); 

[UPDATE]

i thought i need to set flag for intent and properity for activity. therefore, for the activity i want to back to i set

android:launchMode="singleInstance" 

and because, i don't want a new activity from intent, i just want my last activity, so i add

Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 

from documentation i got that using Pendingintent.contentIntent it must include the FLAG_ACTIVITY_NEW_TASK flag and When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. so i also add

Intent.FLAG_ACTIVITY_NEW_TASK 

but from logCat, i still saw that when i touch the notification for back to activity, the pendingintent still called onCreate() function rather than just show the last activity which still "alive".

like image 305
Hsin-Hsiang Avatar asked Aug 25 '13 02:08

Hsin-Hsiang


People also ask

How do I use intent to launch a new activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);


2 Answers

If you want to call the Activity from background try this:

    Intent intent = new Intent(this, YourLauncherActivity.class);     intent.setAction(Intent.ACTION_MAIN);     intent.addCategory(Intent.CATEGORY_LAUNCHER);      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,             intent, 0);     mBuilder.setContentIntent(pendingIntent); 

If you click the Notification while on Homescreen the last shown Activity of your App will be get to the foreground without starting it new. If the Activity was killed by the system you will get a new Activity.

like image 77
Steve Benett Avatar answered Sep 19 '22 16:09

Steve Benett


I have used PendingIntent.getActivities instead of getActivity. This is working well in my project.

Intent backIntent = new Intent(this, HomeActivity.class);                 backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Intent notificationIntent = new Intent(this, NextActivity.class);                  final PendingIntent pendingIntent = PendingIntent.getActivities(this, 1,                         new Intent[] {backIntent, notificationIntent}, PendingIntent.FLAG_ONE_SHOT); 
like image 25
Jitendra ramoliya Avatar answered Sep 18 '22 16:09

Jitendra ramoliya