【问题标题】:Is it a good idea to send data to two fragments from the same activity?将数据从同一个活动发送到两个片段是个好主意吗?
【发布时间】:2018-07-27 19:54:44
【问题描述】:

下面是活动代码的快照。我想将列表发送到单独的片段。第一个工作正常,但是在 stepsFragment.setArguments(stepsBundle);无法识别 setArguments。谢谢。

//
// Send the ingredients array list in Parcelable to the Ingredients Fragment
//
private void sendArrayToIngredientsFragment() {
    //Pack Data in a bundle(call the bundle "ingredientsBundle" to differentiate it from the "stepsBundle"
    Bundle ingredientsBundle = new Bundle();
    ingredientsBundle.putParcelable("Recipes", recipes);

    //Pass Over the bundle to the Ingredients Fragment
    IngredientsFragment ingredientsFragment = new IngredientsFragment();
    ingredientsFragment.setArguments(ingredientsBundle);

    getSupportFragmentManager().beginTransaction().replace(R.id.ingredients_fragment_container, ingredientsFragment).commit();
}

/*
  Send the steps array list in Parcelable to the Steps Fragment
  */
private void sendArrayToStepsFragment() {
    //Pack Data in a bundle(call the bundle "stepsBundle" to differentiate it from the "ingredientsBundle"
    Bundle stepsBundle = new Bundle();
    stepsBundle.putParcelable("Recipes", recipes);

    //Pass Over the bundle to the Steps Fragment
    StepsFragment stepsFragment = new StepsFragment();
    stepsFragment.setArguments(stepsBundle);

    getSupportFragmentManager().beginTransaction().replace(R.id.steps_fragment_container, stepsFragment).commit();
}

}

【问题讨论】:

  • stepsFragment.setArguments 有什么问题?是的,您可以将数据传递给您的片段。在片段类本身中创建一个静态方法的推荐方法是 public static Fragment newInstance() { fragment = ;捆绑捆绑 = 新捆绑(); // 添加数据到 bundle fragment.setArguments(bundle);返回片段; }
  • 消息是:setArguments 无法识别。
  • 如果你还没有找到答案,把StepsFragment的代码贴出来可能会有所帮助
  • 谢谢。我已经想通了。

标签: android android-fragments bundle parcelable


【解决方案1】:

请确保,您的片段类都从片段类扩展。如果是这样,则必须有方法 setArguments()

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多