Android 将数据发送到来自同一活动的两个片段是一个好主意吗?
下面是活动代码的快照。我想把这些列表发送到不同的片段。第一个很好,但是在stepsFragment.setArgumentsstepsBundle中;无法识别setArguments。多谢各位Android 将数据发送到来自同一活动的两个片段是一个好主意吗?,android,android-fragments,bundle,parcelable,Android,Android Fragments,Bundle,Parcelable,下面是活动代码的快照。我想把这些列表发送到不同的片段。第一个很好,但是在stepsFragment.setArgumentsstepsBundle中;无法识别setArguments。多谢各位 // // Send the ingredients array list in Parcelable to the Ingredients Fragment // private void sendArrayToIngredientsFragment() { //Pack Data in a b
//
// 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();
}
}请确保,您的片段类都是从片段类扩展而来的。如果是这样,则必须存在方法setArgumentsstepsFragment.setArguments有什么问题?是的,您可以将数据传递到片段。建议在fragment类本身中创建一个静态方法,即public static fragment newInstance{fragment=;Bundle Bundle=new Bundle;//将数据添加到Bundle fragment.setArgumentsbundle;返回fragment;}消息是:setArguments未被识别。如果您尚未找到答案,发布StepsFragmentThank的代码可能会有所帮助。我已经弄明白了。