Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 216
longi Avatar asked Mar 05 '13 16:03

longi


2 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.

like image 141
android guy Avatar answered Nov 08 '22 00:11

android guy


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" >
    
like image 1
longi Avatar answered Nov 08 '22 00:11

longi