Home » Android » Android: PendingIntent from Notification doesn't trigger the onCreate() if bringing Activity back on Screen

Android: PendingIntent from Notification doesn't trigger the onCreate() if bringing Activity back on Screen

Posted by: admin June 16, 2020 Leave a comment

Questions:

guess I’m having some misunderstandigs with the Intent Flags.. What I’m trying to do is, I’m having a radio streaming applications, which has two Activities (PlayerApplication and SettingsScreen). I have a Service.class for Streaming which runs in the background, which holds a Notification as well (you can stop/start playback in the notification-overlay menu and the PlayerApplication). If the User clicks on the Notification, the PlayerApplicationActivity should be com back to screen.

Everything works fine, expect the case: User opens SettingsScreenActivity -> opens NotificationOverlayMenu -> clicks at Notification -> PendingIntent resumes to PlayerApplicationActivity

Now, PlayerApplicationScreen is there, but I cannot click anything. I see, that the onCreate() of my PlayerApplication isn’t triggered, if I resume from Notification. But the layout looks fine (also inflated in the onCreate())

I used following PendingIntent in the Service.class:

PendingIntent contentIntent = PendingIntent.getActivity(
      this.getApplicationContext(), //Service.class
      0, 
      new Intent(this,PlayerApplicationActivity.class)
             .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP),
      0);

So actually the PendingIntent should bring the Activity (if already running) back to the top. Am I using wrong Intents or am I missing a point?

EDIT:
and I declared launchMode in Manifest.xml

android:launchMode="singleTask"
How to&Answers:

According to android developer site, In FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP and in launch mode : SingleTask , If the instance of activity you are calling is already running then instead of creating new instance, it call onNewIntent() method. So I think you should check this condition that your activity is running when onCreate() is not calling.

Answer:

I just figured out what I was doing wrong:

  1. For my purpose I used the wrong launchMode. Right one is singleTop not singleTask

  2. I declared the launchMode in the <application-node> instead of the <activity-node>

    //Manifest.xml
    <activity
        android:name="..."
        android:launchMode="singleTop" >