How to transfer some data to another Fragment?

前端 未结 10 1926
说谎
说谎 2020-11-22 11:41

How to transfer some data to another Fragment likewise it was done with extras for intents?

10条回答
  •  粉色の甜心
    2020-11-22 12:32

    From Activity Class:

    Send the data using bundle arguments to the fragment and load the fragment

       Fragment fragment = new myFragment();
       Bundle bundle = new Bundle();
       bundle.putString("pName", personName);
       bundle.putString("pEmail", personEmail);
       bundle.putString("pId", personId);
       fragment.setArguments(bundle);
    
       getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        fragment).commit();
    

    From myFragment Class:

    Get the arguments from the bundle and set them to xml

        Bundle arguments = getArguments();
        String personName = arguments.getString("pName");
        String personEmail = arguments.getString("pEmail");
        String personId = arguments.getString("pId");
    
        nameTV = v.findViewById(R.id.name);
        emailTV = v.findViewById(R.id.email);
        idTV = v.findViewById(R.id.id);
    
        nameTV.setText("Name: "+ personName);
        emailTV.setText("Email: "+ personEmail);
        idTV.setText("ID: "+ personId);
    

提交回复
热议问题