How to transfer some data to another Fragment?

前端 未结 10 2081
说谎
说谎 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:25

    Complete code of passing data using fragment to fragment

    Fragment fragment = new Fragment(); // replace your custom fragment class 
    Bundle bundle = new Bundle();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    bundle.putString("key","value"); // use as per your need
                    fragment.setArguments(bundle);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.replace(viewID,fragment);
                    fragmentTransaction.commit();
    

    In custom fragment class

    Bundle mBundle = new Bundle();
    mBundle = getArguments();
    mBundle.getString(key);  // key must be same which was given in first fragment
    

提交回复
热议问题