user2246120
user2246120

Reputation: 1505

How do I know if Activity was opened after clicking on Notification?

I have a MainActivity which displays a list. But when the user clicks on the notification in the status bar, I want to show a dialog on top of MainActivity. Is it possible to query if the Activity started through a Notification?

I also tried to add extra information to the intent to let the activity know it should pop up that dialog. But the bundle is always null. And adding this to onResume() doesn't make sense since the Activity could already be visible.

    long id = intent.getLongExtra("id", 0);
    String title = intent.getStringExtra("msg");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.drawable.ic_action_ic_action_edit);
    mBuilder.setContentTitle("EasyReminder");
    mBuilder.setContentText(title);
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resultIntent.putExtra("ShowDialog", true);

     PendingIntent pi = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager = 
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify((int)id, mBuilder.build());

MainActivity.java

@Override
public void onResume() {
    super.onResume();
    // this is always null
    Bundle extras = getIntent().getExtras();
    if(extras != null) {
        DialogPopupFragment fr = new DialogPopupFragment();
        fr.show(getFragmentManager(), "DialogPopupFragment");
    }
}

Upvotes: 1

Views: 462

Answers (1)

yoah
yoah

Reputation: 7230

Try to set flag FLAG_ACTIVITY_SINGLE_TOP on resultIntent, and implement method onNewIntent() in MainActivity

Upvotes: 2

Related Questions