singleTask模式的Activity接收Bundle问题(深入了解LaunchMode)

  项目里使用了第三方的SDK BuzzBox来实现Notification的通知推送功能。需要实现通知推送后,跳转到MainActivity,并根据点击对应NotificationMessage,在主界面显示特定Fragment。因此需要实现通知点击跳转的类并附带参数传递。
Task class中:

    notification.setNotificationClickIntentClass(YourMainActivity.class);
    Bundle b = new Bundle();
    b.putString("param", "value");
    notification.setNotificationClickIntentBundle(b));     

而一开始在MainActivity里直接这么接收Bundle:

    Bundle b = getIntent().getExtras();

结果一直是个null。
  理论上把Bundle参数传给了MainActivity,那么为什么收不到呢?其实问题在于Activity的singleTask模式中Bundle没有更新 。因为MainActivity并没有销毁,所以原来就有intent,而丢弃了新来intent,因此只要处理新来的intent就能取到参数了。方法是重写接收端Activity的onNewIntent()

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    protected void onResume() {
        super.onResume();
        Bundle b = getIntent().getExtras();
    }

参考:
1.http://blog.csdn.net/dadoneo/article/details/8170124

posted @ 2015-01-29 18:38  东方小虾米  阅读(141)  评论(0编辑  收藏  举报