22

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".

Hsin-Hsiang
  • 411
  • 1
  • 3
  • 13
  • i got an solution from http://stackoverflow.com/questions/2424488/android-new-intent-starts-new-instance-with-androidlaunchmode-singletop but no luck :( – Hsin-Hsiang Aug 25 '13 at 02:45

5 Answers5

48

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.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • But the activity which backed by this way will separate from my original application :( i.e. activity(A) -> activity(B) at the same time do PendingIntent for B. When i back to B from notification, i can't return to A anymore... – Hsin-Hsiang Aug 25 '13 at 07:58
  • 2
    That's strange. Are you still using `android:launchMode="singleInstance"`? – Steve Benett Aug 25 '13 at 08:06
  • yes. i didn't modified that. Do i need to set parentActivity for this Activity which used in PendingIntent? – Hsin-Hsiang Aug 25 '13 at 08:10
  • i added the for my minSDKVersion < 16 but no luck, then tried add the TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(ResultActivity.class); stackBuilder.addNextIntent(resultIntent); but still no luck... the situation like this link http://stackoverflow.com/questions/13632480/android-build-a-notification-taskstackbuilder-addparentstack-not-working – Hsin-Hsiang Aug 25 '13 at 08:37
  • anyway, thanks for your great help again. i think this is another problem i need to fix :) – Hsin-Hsiang Aug 25 '13 at 08:49
  • @SteveBenett if the user swipes away my app from recent screens then clicks on a notification with this code in it, the app crashes. Do you know how to fix this? –  May 02 '15 at 10:14
3

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);
Jitendra
  • 3,608
  • 2
  • 17
  • 19
2

For me, this worked :

.setContentIntent(
     PendingIntent.getActivity(
     context,
     0,
     new Intent(context, YOUR_MAIN.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP ),PendingIntent.FLAG_ONE_SHOT))   

FLAG_ONE_SHOT : DO NOT CREATE TWICE (associated with Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)

Jeffreez
  • 21
  • 3
1

In SingleTop mode, I use this method without define explicitly the root activity class:

Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
.setComponent(getPackageManager().getLaunchIntentForPackage(getPackageName()).getComponent());

and this to resume last activity from notification:

builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));

or this to resume last activity from a service:

try {
    PendingIntent.getActivity(this, 0, intent, 0).send();
} catch (CanceledException e) {
    e.printStackTrace();
}
alex
  • 5,661
  • 6
  • 33
  • 54
0

Code hear

        Intent resultIntent = new 
 Intent(getApplicationContext(),MainActivity.class);

        resultIntent.putExtra("splash",2);
        resultIntent.putExtra("message","test");
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationCompatBuilder.setContentIntent(notifyPendingIntent);
  
    Intent intent = this.getIntent();
    Bundle extras = intent.getExtras();
 
    if (extras != null) {
        String extraValue = extras.getString("notificationType");

        if(extraValue !=null){
            int value = Integer.parseInt(extraValue);
            if (value == 1) {
                intent = new Intent(SplashActivity.this,MainActivity.class);
                intent.putExtra("splash",value);
                intent.putExtra("message", "fromsplash");
                startActivity(intent);
                finish();
            } else
             if (value == 2){
                 intent = new Intent(SplashActivity.this,MainActivity.class);
                 intent.putExtra("swip","2");
                 intent.putExtra("message", "test");
                startActivity(intent);
                finish();
            } 
        } 

    } 
}