包含Bundle的Activity

时间:2017-08-19 07:49:14

标签: android bundle

如何检查活动是否包含捆绑包?
这是允许我在初始Activity

中存储值的代码
   Intent i = new Intent(Activity1.this,Activity2.class);
   Bundle bundle = new Bundle();
   bundle.putStringArray("contact_to_bundle", new String[]{nomStr, prenomStr, numeroStr, imageStr});
   i.putExtras(bundle);

  startActivity(i);

2 个答案:

答案 0 :(得分:0)

bundle.containsKey()是检查活动是否包含任何包的方法。 用此替换代码以检查空包:

发送包的活动:

 Intent i = new Intent(Activity1.this,Activity2.class);
 bundle.putStringArray("contact_to_bundle", new String[]{nomStr,
                    prenomStr, numeroStr, imageStr});
 i.putExtras(bundle);
 startActivity(i);

抓取包的活动:

 Bundle bundle = new Bundle();
 bundle=getIntent().getExtras;
    if (bundle != null) {
        if (bundle.containsKey("contact_to_bundle")) {
            //fetch values
            bundle.getStringArray("contact_to_bundle");

        }
    }

答案 1 :(得分:0)

希望这有助于您: 像这样启动你的Activity2:

   Intent i = new Intent(Activity1.this,Activity2.class);
   Bundle bundle = new Bundle();
   bundle.putStringArray("contact_to_bundle", new String[]{nomStr, prenomStr, numeroStr, imageStr});
   i.putExtras(bundle);
   startActivity(i);

然后在Activity2的onCreate方法中检查以下内容:

Bundle bundle = getIntent().getExtras();
if (bundle != null){
      //Your activity contains bundle.
      //Check for values in your bundle
      if (bundle.containsKey("contact_to_bundle")){
            bundle.getStringArray("contact_to_bundle");
      }
} else {
     //Your activity not contains bundle.
}