Android设置launchMode为singleTask的Activity怎么刷新页面内容

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

    Intent intent = new Intent(this,  ActivityB.class);  

    intent.putExtra("name", mUserName);

    startActivity(intent);  


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

    Bundle bundle = getIntent().getExtras();  

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

        mUserName = bundle.getString("name");  

    }   


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


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


@Override  

protected void onNewIntent(Intent intent) {        

    super.onNewIntent(intent);  

    setIntent(intent); 

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值