Skip to content Skip to sidebar Skip to footer

Pendingintent Completion

I record this in the service: intent = new Intent(this,MainActivity_Large.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_

Solution 1:

There are some Activity methods which will help you to find out whether the Activity comes to the foreground because the user tapped the notification:

getIntent() will give you the Intent which triggered Activity creation if tapping the notification happens when the Activity has to be (re)created.

Overriding onNewIntent() will give you access to the Intent responsible for re-launching an existing Activity. Note that you also have the opportunity to set the new Intent as "the" Intent for this Activity:

@OverrideprotectedvoidonNewIntent(Intent newIntent){
    setIntent(newIntent);
}

So you can put a boolean as Intent extra when creating the Intent for your PendingIntent ...

intent = newIntent(this,MainActivity_Large.class);
intent.putExtra("FROM_NOTIFICATION", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP); 

... and evaluate this in your code, e.g. in Activity.onResume()

@overrideprotectedvoidonResume(){
    if(getIntent().hasExtra("FROM_NOTIFICATION")){
        // Activity was (re-)launched because user tapped notification
    }
}

I'm aware that this way you won't be able to tell when the user taps the notification a second time when the Activity is already up and running. Since I don't know your use case I can't tell whether this can happen or whether this would be a problem. But since you can also pass other data as Intent extra, it should be possible to identify each event if required.

Post a Comment for "Pendingintent Completion"