Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Latest Intent

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);

}
like image 715
coderplus Avatar asked Jan 26 '26 08:01

coderplus


2 Answers

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.

like image 147
waqaslam Avatar answered Jan 28 '26 21:01

waqaslam


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.

like image 30
Felipe Queiroz Avatar answered Jan 28 '26 22:01

Felipe Queiroz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!