GetIntent () in onResume () always returns the same action, how to use it?

I am showing a notification with this intention:

Intent intentOpen = new Intent(this, MainActivity.class);
intentOpen.setAction("ACTION_SHOW_BACKUP_FRAGMENT");
intentOpen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntentOpen = PendingIntent.getActivity(this, 0, intentOpen, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntentOpen);

As you can see, the action is set to "ACTION_SHOW_BACKUP_FRAGMENT", so when the user clicks on the notification, my main singleTop MainActivity can get the action in the method onResume()with getIntent().getAction().

For this to work, I had to implement it onNewIntent()as follows:

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

, , , . , "", , , onResume() , "ACTION_SHOW_BACKUP_FRAGMENT" ! , .

Intent PendingIntent, . setIntent(new Intent()); onResume(), onNewIntent() "ACTION_SHOW_BACKUP_FRAGMENT" .

?

+4
1

, Intent PendingIntent:

// Create pending intent to open the backup fragment
intentOpen.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntentOpen = PendingIntent.getActivity(this, 0, intentOpen, PendingIntent.FLAG_CANCEL_CURRENT);

, onResume() setIntent(null);

+2

Source: https://habr.com/ru/post/1545489/


All Articles