-2

My app is simple just i have a FragmentA and FragmentB first one has a single button pass String data ("Hi i'm A") through second fragment who has a text to show this message My question is

Why my "ModifyTxt" method doesn't work ?

public class newFragmentA extends Fragment{
    Button button;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.newfragmenta_layout,container,false);
    }

    newFragmentB newfragmentB;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = getActivity().findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newfragmentB.ModifyTxT("Hi I'm A");
            }
        });


       }
}

And this is my second Fragment(FragmentB)

public class newFragmentB extends Fragment {
    TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.newfragmentb_layout,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView = getActivity().findViewById(R.id.TV);
    }
    public void ModifyTxT(String string){
        textView.setText(string); }
}
  • https://stackoverflow.com/questions/13700798/basic-communication-between-two-fragments possible duplicate of this problem. – avez raj Aug 02 '18 at 09:51

2 Answers2

0

Try this:

public class FragmentB extends Fragment {
  // ...

  public static FragmentB newInstance() {
        return new FragmentB();
    }
}

In FragmentA :

button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    FragmentB newFragment = FragmentB.newInstance();
    newFragment.modifyTxT("Hi I'm A");
  }
});

Be careful to coding rules : uppercase first letter for class name, lowercase first letter for method...

Bruno
  • 3,872
  • 4
  • 20
  • 37
0

Prefer using Interface to communicate with Fragments

public class FragmentA extends Fragment{
    public interface MyCallback{
        void modifyTxT(String text); // method naming convention start with small letter.
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = getActivity().findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (newfragmentB instanceof MyCallback) {
                    ((MyCallback)newfragmentB).modifyTxT("Hi I'm A");
                }
            }
        });


    }

}

public  class FragmentB extends Fragment implements FragmentA.MyCallback{

    public static FragmentB getInstance(){
        return new FragmentB();

    }

    @Override
    public void modifyTxT() {

    }
}
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73