Android 获取活动';s 1活动的额外目的';s2静态片段
我有下面的场景。我使用的意图是从Android 获取活动';s 1活动的额外目的';s2静态片段,android,android-intent,fragment,android-bundle,Android,Android Intent,Fragment,Android Bundle,我有下面的场景。我使用的意图是从SelectRecipeActivity转到recipeSecativity。我用一捆东西来传递配方成分和步骤RecipeStepsActivity承载一个静态片段(RecipeStepsFragment),其中显示配方的成分和步骤。我的问题是,将意图包传递给RecipeStepsFragment的最佳实践是什么 现在我在RecipeStepsFragment的onCreateView()中使用getActivity().getIntent().getExtras
SelectRecipeActivity
转到recipeSecativity
。我用一捆东西来传递配方成分和步骤RecipeStepsActivity
承载一个静态片段(RecipeStepsFragment
),其中显示配方的成分和步骤。我的问题是,将意图包传递给RecipeStepsFragment
的最佳实践是什么
现在我在RecipeStepsFragment
的onCreateView()
中使用getActivity().getIntent().getExtras()
从SelectRecipeActivity
中获取意图的额外内容,它可以正常工作
由于它不是一个动态片段(我不使用片段构造函数或newInstance方法,它是使用
标记在xml中声明的),并且没有发生片段事务,因此我无法使用片段参数传递附加值,我知道这是推荐的方法。或者我可以吗?我错过什么了吗?谢谢 好的,我假设您只是指在xml中由“static”定义的片段,而不是静态变量(这对于片段来说是非常糟糕的)。在这种情况下-给片段一个id,在活动的onCreate中使用findFragmentById,将其转换为正确的片段类型,并调用片段上的函数来传递适当的数据。遵循@Gabe Sechan的建议,我使用以下方法将包从RecipeStepsActivity
传递到RecipeStepsFragment
1) 我从SelectRecipeActivity
到recipeSecativity
的onCreate
方法接收到额外的意图
2) 在RecipeStepsActivity
的onCreate
方法中,我通过调用findFragmentById
获得对RecipeStepsFragment
的引用,如下所示:RecipeStepsFragment stepsFragment = (RecipeStepsFragment)getSupportFragmentManager()
.findFragmentById(R.id.master_steps_fragment);
3) 然后我得到创建一个捆绑包的意图附加,然后将其作为RecipeStepsFragment
的参数传递,如下所示:
Bundle args = getIntent().getExtras();
//Pass the intent extras to the fragment using a bundle
if (args != null) {
//show Dessert Name in Toolbar
mRecipe = args.getParcelable(EXTRAS_RECIPE_ITEM);
assert mRecipe != null;
setTitle(mRecipe.getName());
assert stepsFragment != null;
stepsFragment.setArguments(args);
}
Bundle fragmentArgs = getArguments();
4) 现在在RecipeStepsFragment
的->onActivityCreated