重新加载相同的活动但通过捆绑

时间:2013-09-17 08:36:58

标签: android android-activity bundle reload

我正在尝试重新加载我的活动并传递bundle,但我得到一个空(null)包。

重新加载活动:

Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
MyActivity.this.finish();
startActivity(intent);

onCreate活动,我应该获得bundle

@Override
public void onCreate(Bundle savedInstance)
{
   if (savedInstance != null)
   {
   }
   else
   {
      Log.i("d", "IS NULL !");
   }
}

我变空了。

2 个答案:

答案 0 :(得分:5)

OnCreate()中你应该这样做:

if(getIntent().getExtras() != null) {
    Bundle extras = getIntent().getExtras();
    Log.i("Value", extras.getString("key"));
}

而不是这个

if (savedInstance != null){
}

答案 1 :(得分:2)

首先启动活动,然后按如下方式调用finish()

Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
startActivity(intent);
MyActivity.this.finish();

然后收到这样的附加内容:

Bundle bundle = getIntent().getExtras();

最后,您可以设置条件来检查它是否正确如下:

if(bundle != null)
{
}
else
{
   Log.i("d", "IS NULL !");
}
相关问题