在需要启动另一个activity,并传递一些数据时,我们常采取如下的方法:

Intentintent =newIntent(this, ActivityB.class);

intent.putExtra("name", mUserName);

startActivity(intent);


同时在ActivityB中的onCreate()或onResume()方法中获取传递的数据:

Bundlebundle =getIntent().getExtras();

if(bundle!=null&&bundle.containsKey("name")){

mUserName =bundle.getString("name");

}


但是,当把ActivityB的加载方式设置为singleTask或singleInstance时,我们会发现,除了第一次能正确接收以外,其他的好像都是为空?


原来,activity的getIntent()方法只是获取activity原来的intent。因此要想解决上述问题,可采用的办法之一是重载onNewIntent()方法。


@Override

protectedvoidonNewIntent(Intentintent){

super.onNewIntent(intent);

setIntent(intent);

//here we can use getIntent() to get the extra data.

}

更多相关文章

  1. MVP模式
  2. Android(安卓)web界面丝滑进度条
  3. Android——多线程(AsyncTask封装)
  4. MPAndroidChart项目实战——MarkerView显示问题解决
  5. DialogFragment的使用总结
  6. android 常见面试题(三)
  7. TrafficStats ------- Android流量统计类的使用
  8. 新博andorid 初中级考试评测以及答案-------小林老师出的试卷
  9. Android文件、内存、SDCard管理常用工具类、方法

随机推荐

  1. 关于Android菜单上的记录
  2. android仿今日头条App、多种漂亮加载效果
  3. 开放平台的Android SDK
  4. android Switch控件
  5. Android日记之2012/02/10——Android中的
  6. Android(安卓)NDK开发详细介绍
  7. Android真响应式架构——数据流动性
  8. Android 上 Https 双向通信— 深入理解Ke
  9. android 实现自定义键盘的实例
  10. android 使用Intent传递数据之全局变量传