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.

}

你可能感兴趣的:(android)