我将数据从"活动"结转到"片段"。但是,我无法使用getArguments()
从捆绑包中检索数据。
这是我的抽屉活动代码:
case R.id.nav_home:
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
HomePageFragment homePageFragment = new HomePageFragment();
homePageFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, homePageFragment).commit();
break;
这是我的家庭片段代码:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
Bundle bundle = getArguments();
String strtext = bundle.getString("edttext");
return inflater.inflate(R.layout.fragment_google_map, container, false);
}
您可以尝试在onViewCreated(…(中检索它吗?
编辑活动代码-
FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final MyFragment myFragment = new MyFragment();
Bundle b = new Bundle();
b.putString("message", editText.getText().toString());
myFragment.setArguments(b);
fragmentTransaction.add(R.id.frameLayout, myFragment).commit();
编辑主页片段-
Bundle bundle = getArguments();
String message = bundle.getString("message");
textView.setText(message);