【问题标题】:Reload same activity but pass bundle重新加载相同的活动,但通过捆绑
【发布时间】:2013-09-17 08:36:57
【问题描述】:

我正在尝试重新加载我的活动并传递一个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 !");
   }
}

我得到了空值。

【问题讨论】:

标签: android android-activity bundle reload


【解决方案1】:

OnCreate() 你应该这样做:

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

不是这个

if (savedInstance != null){
}

【讨论】:

    【解决方案2】:

    先启动activity,然后调用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 !");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多