开发者

passing variable to an activity through an Intent is always the same

My application receives a C2DM message and sends a status bad notification with the C2DM message. So far so good. When the user clicks on the notification, an activity is called, passing the C2DM message as a variable.

Now, the first time it works smoothly, the second time the variable passed is not refreshed. It's always the first variable passed. Am I missing something?

Here are the snipperts:

C2DM Notification

Intent notificationIntent = new Intent(context, BMBPad.class);
notificationIntent.putExtra("seqid", message);              
Pend开发者_运维问答ingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

This is how I read the variable in the Activity called by the Intent.

extra = this.getIntent().getExtras();
seqidi = extra.getString("seqid");

Anyone any idea why that happens?


You need to use the flag PendingIntent.FLAG_UPDATE_CURRENT

In your case:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Please also have a look here: Android PendingIntent


You can try to append this snippet in the Activity called by the Intent.

/**
 * Override super.onNewIntent() to let getIntent() fetch 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);
}


override onNewIntent() method, get your variable like this:

@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
seqid = intent.getStringExtra("seqid","");

}

because start the activity again will trigger onNewIntent() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜