【问题标题】:Add a button dynamically from one fragment to another [duplicate]将按钮从一个片段动态添加到另一个[重复]
【发布时间】:2018-02-07 10:58:44
【问题描述】:
【问题讨论】:
标签:
java
android
android-fragments
【解决方案1】:
目前尚不清楚它是如何在应用程序中实现的。假设两个片段在活动中同时显示。在这种情况下,您可以使用FragmentManager 获取对另一个片段的引用:
((MyFragment) getFragmentManager().findFragmentById(...)).addButton(myArgs)
如果它们被放置在不同的活动中或者如果一个替换另一个,则需要使用参数Bundle在两者之间传递信息:
SecondFragment secondFragment = new SecondFragment();
Bundle args = new Bundle;
args.putString("name", "batman");
secondFragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.container, secondFragment).commit();
// later in SecondFragment#onViewCreated
button.setText(getArgments.getString("name"));