public class MainActivity extends AppCompatActivity {
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Toast.makeText(this, "onNewIntent", Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent a = new Intent(this, MainActivity.class);
Intent b = new Intent(this, MainActivity.class);
b.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pia = PendingIntent.getActivity(this, 1, a, 0);
PendingIntent pib = PendingIntent.getActivity(this, 1, b, PendingIntent.FLAG_UPDATE_CURRENT);
try {
pib.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
With above code, when Activity starts, instead of an "onNewIntent
" toast pops up, a new MainActivity invoked, ignores FLAG_ACTIVITY_SINGLE_TOP
flag.
But if I change FLAG_UPDATE_CURRENT
to FLAG_CANCEL_CURRENT
, problem solved.
Is this a bug of Android? I'm testing this on an emulator running Android Pie.
Is there any way to update PendingIntent's flag?
When you use PendingIntent.FLAG_UPDATE_CURRENT
this only causes the "extras" in the Intent
to be updated. It does nothing to the Intent
flags.
If you want to change the Intent
flags, you need to either use 2 different PendingIntent
s (by using different requestCode
s, for example), or you need to cancel the existing PendingIntent
and create a new one.