Android intent 传值不更新的原因和解决办法

当 Activity 的启动模式是 singleTask 或者 singleInstance 的时候。如果使用了 intent 传值,则可能出现 intent 的值无法更新的问题。也就是说每次 intent 接收到的值都是第一次接到的值。因为 intent 没有被更新。想要更新需要做两件事情。

1. 发送方 Activity,加上一句话

PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);  

2. 接收方 Activity,加上一个函数,调用方法 setIntent

protected void onNewIntent(Intent intent) {
  Log.i(TAG,"onNewIntent()");
  super.onNewIntent(intent);
  setIntent(intent);
  int value = getIntent().getIntExtra("value_key", 0);
  Log.i(TAG,"value = "+value);
}
posted @ 2014-05-01 22:53  davesuen  阅读(1654)  评论(0编辑  收藏  举报