3

I'm Invoking an Activity from my Service. I do this whenever an event occurs and every time I pass a Serializable object via the Intent. The problem here is that when the Activity is invoked for the second time, it has the old Intent data and not the new one. So I'm sure that this is due to some mistake that I have made in the Activity class but I'm not able to figure it out.

public class ReceiveActivity extends Activity {
AlertDialog alertDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Event");
    CustomEvent custom= (CustomEvent) getIntent().getSerializableExtra("custom");
    alertDialog.setMessage(custom.getName());
    alertDialog.setIcon(R.drawable.ic_launcher);
    alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            ReceiveActivity.this.finish();
        }
    });
    alertDialog.show();
}

@Override
protected void onPause() {
    if(alertDialog!=null) {alertDialog.dismiss();}
    super.onPause();

}

@Override
protected void onStop() {
    if(alertDialog!=null) {alertDialog.dismiss();}
    super.onStop();

}

and this is the code that I use to Invoke the Activity form the Service(Via a Notification)

Notification notification = new Notification(R.drawable.ic_launcher, "msg", 
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
Intent incoming =new Intent(this, ReceiveActivity.class);
incoming.putExtra("custom",custom);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,incoming, 0);
notification.setLatestEventInfo(this, "msg","incoming", contentIntent);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("Incoming", count++,notification);

}
coderplus
  • 5,793
  • 5
  • 34
  • 54

3 Answers3

4

I think you need to override onNewIntent method to get the afterwards comming intents. It happens if your activity's launch mode is set to singleTop and the activity is not finished till it gets the second intent.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • I just did that but it doesn't always work.I haven't specified any launch modes. So it's `standard` – coderplus May 22 '12 at 19:59
  • I changed the launchMode to `singleTop`, set the flag in the intent, but it still doesn't work. The overridden method `onNewIntent(..)` is not being invoked. – coderplus May 22 '12 at 20:15
  • try setting launch mode to **singleInstance** – waqaslam May 22 '12 at 20:21
  • If I'm directly invoking the `Activity` it's working as expected. So I think there is some error in the way I'm creating the `Notification` – coderplus May 22 '12 at 20:36
  • the problem is with [PendingIntent](http://stackoverflow.com/questions/3140072/android-keeps-caching-my-intents-extras-how-to-declare-a-pending-intent-that-ke) but using FLAG_UPDATE_CURRENT will lead to loss of old intents(if notifications aren't received and opened in order) – coderplus May 22 '12 at 20:52
  • Thanks a lot for all the help. The solution was to add distinct actions to each intent using `intent.setAction(string)` – coderplus May 22 '12 at 21:05
3

Try to do with onResume() method because the Activity is not created again. It only switch places with the top Activity.

The Android only finishes the Activity when it needs memory.

Felipe Queiroz
  • 461
  • 5
  • 18
1
/**
 * Override super.onNewIntent() so that calls to getIntent() will return the
 * latest intent that was used to start this Activity rather than the first
 * intent.
 */
@Override
public void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    setIntent(intent);
}
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • my onNewIntent() never get called, can you please share with me any idea? – Beeing Jk Dec 16 '15 at 07:16
  • Does you class extend `Activity`? And is the activity definitely being started - e.g., is the activity's `onCreate()` method called? – ban-geoengineering Dec 16 '15 at 15:11
  • Have you set the launch mode of the activity to "singleTop" - see http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29 – ban-geoengineering Dec 17 '15 at 11:00
  • yes I've tried singleTop and singleTask, I actually have asked a [question](http://stackoverflow.com/questions/34304111/android-always-got-the-old-intent-when-opening-links-from-browser-through-recen) regarding this but it hasn't been solved yet – Beeing Jk Dec 18 '15 at 04:00
  • Not sure then. If you add a bounty to your question, I expect some more suggestions will come in. :-) – ban-geoengineering Dec 22 '15 at 13:53