14

I have one Android application, when it runs a service, I want to show the notification on the status bar. Then the user could navigate to other application by pressing HOME key. However when I try to bring the previous running application back to Front via notification icon, there is some problem with the existing activity. Even I declare it as "Single Top" mode (I want to run the existing activity since there is an associated service running) , somehow that activity's OnDestroy has been called before OnResume. Here is my code of creating the notification object. Could you please point me what wrong it is. Thanks.

private void showNotification ()
{

  Intent toLaunch = new Intent(getApplicationContext(),
                                          MySingleTopActivity.class);

  PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(),   0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);

  notification.setLatestEventInfo(getApplicationContext(),
         getText(R.string.GPS_service_name), text, intentBack);
....
}
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
user404012
  • 141
  • 1
  • 1
  • 3
  • possible duplicates: http://stackoverflow.com/questions/4047683/android-how-to-resume-an-app-from-a-notification, http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification – Philipp Feb 17 '13 at 04:12

4 Answers4

23
private void showNotification() {
    Intent toLaunch = new Intent(getApplicationContext(), MySingleTopActivity.class);

    //add these two lines will solve your issue
    toLaunch.setAction("android.intent.action.MAIN");
    toLaunch.addCategory("android.intent.category.LAUNCHER");

    PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0, toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack);
    ...
}
Amir Latifi
  • 798
  • 8
  • 15
santhosh
  • 329
  • 3
  • 4
  • notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack); text should be toLaunch i think.. – Siddhesh Jan 16 '13 at 08:27
  • +1 with the mention that the PendingIntent declaration should be `PendingIntent contentIntent = PendingIntent.getActivity(this, 0, toLaunch, 0);` – Calin Oct 23 '13 at 09:39
8

I'd recommend you do this,

private void showNotification () 
{

Intent toLaunch = new Intent(getApplicationContext(), DummyActivity.class);

// You'd need this line only if you had shown the notification from a Service
toLaunch.setAction("android.intent.action.MAIN");

PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);

.... 
}

The DummyActivity should simply be an activity that always finishes itself in the oncreate event.

In the manifest file, add these lines

<activity class=".DummyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

I hope this helps ...

0

Another way to launch your package intent.

private void NotificationwithLaucherSelfPackage(Context context , int notification_id){
        Notification noti = new Notification.Builder(context)
                .setContentTitle("Your Title")
                .setContentText("Your Text")
                .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
                .setContentIntent(PendingIntent.getActivity( context ,notification_id , getLauncherIntent(context) , PendingIntent.FLAG_UPDATE_CURRENT))
                .build();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    }

private Intent getLauncherIntent(Context context){
        return context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    }
Deyu瑜
  • 484
  • 4
  • 14
-2

When an Activity is in the background, Android may kill the activity at any time to free resources. See the Activity documentation for complete details on the lifecycle.

Your activity needs to be prepared to save its state in the onPause or onSaveInstanceState methods. The document referenced above has additional details on saving persistant state.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • 1
    I don't think the activity should be called by "onDestroy". Only "onPause" is called when it goes to background. And "onResume" is called when it is back to frontend. If I simply click on the application icon to re-start the application, there is no problem, only "OnResume" is called. However if I try to click on the notification icon, the activity goes "onDestroy" -> "OnResume", that causes problem. – user404012 Jul 30 '10 at 05:45
  • onPause is called when it goes into the background. However, as its states in the referenced doc, Android may choose to kill the Activity at any time if it needs the resources, and thus onDestroy will be called. You cannot depend on the fact that onDestroy will not be called, thus you need to deal with it properly by saving state. – Cheryl Simon Aug 02 '10 at 16:01