5 回答
TA贡献1802条经验 获得超6个赞
在代码中,您将捆绑包设置为未使用的片段变量
创建捆绑包
创建片段
在片段中设置捆绑包
显示片段
这是您需要以片段形式传递参数的方式
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragment = new SettingsFragment();
if (savedInstanceState == null) {
Bundle bundle = new Bundle();
bundle.putString("my_bundle_key", "Bundle");
settingsFragment.setArguments(bundle);
}
else {
settingsFragment.setArguments(savedInstanceState);
}
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, settingsFragment)
.commit();
TA贡献1821条经验 获得超5个赞
在调用之前,您必须调用捆绑包,如下所示:fragment
Bundle bundle = new Bundle();
bundleSettings.putString("my_bundle_key", "Bundle");
SetttingsFragment setttingsFragment = new SetttingsFragment();
setttingsFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, setttingsFragment)
.commit();TA贡献1805条经验 获得超9个赞
在您的活动中,您正在创建两个片段对象,您正在执行的第二个对象未附加到视图且未在使用中。您的片段应按如下方式附加到视图:setargument
Bundle bundle = new Bundle();
bundleSettings.putString("my_bundle_key", "Bundle");
SetttingsFragment setttingsFragment = new SetttingsFragment();
setttingsFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, setttingsFragment)
.commit();TA贡献1795条经验 获得超7个赞
From Activity you send data with intent as:
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
Fragmentclass obj = new Fragmentclass();
obj .setArguments(bundle);
在片段创建视图方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
TA贡献1812条经验 获得超5个赞
活动
AbcFragment fragment = AbcFragment .newInstance(**Value**);
getSupportFragmentManager.beginTransaction()
.replace(R.id.layout_container, fragment)
.commit();
在片段中
public static AbcFragment newInstance(String Value) {
Bundle args = new Bundle();
args.putString("Value", Value);
AbcFragment fragment = new AbcFragment ();
fragment.setArguments(args);
return fragment;
}
添加回答
举报
