3

我正在Activity从我的Service. 每当发生事件以及每次通过 Intent 传递 Serializable 对象时,我都会这样做。这里的问题是,当第二次调用 Activity 时,它有旧的 Intent 数据而不是新的 Intent 数据。所以我确信这是由于我在Activity课堂上犯了一些错误,但我无法弄清楚。

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

}

这是我用来从服务中调用活动的代码(通过 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);

}
4

3 回答 3

4

我认为你需要重写onNewIntent方法来获得后来的意图。如果您的活动的启动模式设置为singleTop并且活动在获得第二个意图之前尚未完成,则会发生这种情况。

于 2012-05-22T19:45:02.780 回答
3

尝试使用 onResume() 方法,因为没有再次创建 Activity。它只与顶部 Activity 交换位置。

Android 仅在需要内存时才完成 Activity。

于 2012-05-22T19:45:38.053 回答
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);
}
于 2014-10-30T12:28:31.473 回答