将数据发送到来自同一活动的两个片段是否是一个好主意?

时间:2018-07-27 19:54:45

标签: android android-fragments bundle parcelable

下面是活动代码的快照。我想将列表发送到单独的片段。第一个可以正常工作,但是在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();
}

}

1 个答案:

答案 0 :(得分:1)

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

相关问题