Question

Unfortunately, I was unable to find a resolution to this issue in other StackOverflow posts so I apologize in advance for re-posting this question. Essentially, I have an AppWidget that creates a Notification. I want the user to click the Notification to launch an Activity. The AppWidget and Activity are in the same APK.

The following code is all in the AppWidget. Clicking on the Notification broadcasts a custom Intent which is received by the AppWidget.

The problem is that the Activity does not appear to launch, even though logcat demonstrates that the custom Intent has indeed been broadcast. The only clue is the logcat message reiterated in the post title.

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Constants.BROWSER_INTENT)) {
        launchActivity(context, R.layout.appwidget, R.id.launch_button, Activity.class);
    }
    super.onReceive(context, intent);
}


private void displayNotification(Context context, String ticker, String title, String msg) {
    Intent intent = new Intent(context, ThisWidget.class);
    intent.setAction(Constants.BROWSER_INTENT); 
    PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon, ticker, System.currentTimeMillis());

    notification.setLatestEventInfo(context, title, msg, pending);
    notificationManager.notify(1, notification);
}

Some notes:

  • Pressing a button to execute launchActivity() works (it launches the Activity)
  • launchActivity uses Intent.FLAG_ACTIVITY_NEW_TASK as recommended by Android lifecycle documentation

Has anyone solved this issue when launching an Activity from a Notification press?

Was it helpful?

Solution

er, figures, I answered my own question just minutes after posting it. I guess I was over-complicating the implementation by using an Intent action. I changed these lines:

   Intent intent = new Intent(context, ThisWidget.class);
   intent.setAction(Constants.BROWSER_INTENT); 
   PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

to this:

   Intent intent = new Intent(context, Activity.class);
   PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

and it worked.

Sorry for the bogus post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top