해보자

[Android] Fragment 간의 Data전달 본문

ANDROID

[Android] Fragment 간의 Data전달

안댕 2020. 2. 11. 22:22
A Fragment → Activity → B Fragment

 

A Fragment 

  Bundle bundle = new Bundle();

  bundle.putString("data_1", (String)data1); 

  bundle.putString("data_2", (String)data2); 



  (MainActivity)getActivity()).setFragment(bundle, BFragment.newInstance());

 

MainActivity

 public void setFragment(Bundle bundle, Fragment fragment) {

        Fragment ft = getSupportFragment().beginTransaction();

        fragment.setArguments(bundle);

        ft.replace(R.id.main_frame, fragment);

        ft.commit();

  }

 

B Fragment 

  if ( getArguments() != null ) {

       String data1 = getArguments().getString("data_1");

       String data2 = getArguments().getString("data_2");

  }