0

I cant pass the data from FragmentOne to FragmentTwo it only move to FragmentTwo without the text i already type it in the edit text in FragmentOne i did my search but i cant any solution that can fix the error.

This is the MainActivity

public class FragmentExampleAct extends AppCompatActivity implements FragmentOne.OnNameSetListener   {
    RelativeLayout rl;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_example);
        rl=(RelativeLayout)findViewById(R.id.rlTwo);


        FragmentOne f1=new FragmentOne();
        getSupportFragmentManager().beginTransaction().replace(R.id.rlTwo,f1).commit();


    }

    @Override
    public void setName(String name) {

         FragmentTwo fragment = (FragmentTwo) getSupportFragmentManager().findFragmentById(R.id.f2);

         if(fragment !=null)
         {
             fragment.updateName(name);
         }
         else
         {
             FragmentTwo fragmentTwo=new FragmentTwo();
             Bundle args=new Bundle();
             args.putString("text",name);
             fragmentTwo.setArguments(args);
             getSupportFragmentManager().beginTransaction().replace(R.id.rlTwo,fragmentTwo).addToBackStack(null).commit();
             //fragmentTwo.updateName(name);

         }




    }
}

This is FragmentOne

public class FragmentOne extends Fragment implements View.OnClickListener{
    Button btn;
    EditText etName;
    OnNameSetListener onNameSetListener;



    public FragmentOne() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View v= inflater.inflate(R.layout.fragment_fragment_one, container, false);
       btn=(Button) v.findViewById(R.id.button4);
       etName=(EditText) v.findViewById(R.id.editText5);
       btn.setOnClickListener(this);
       return v;
    }

    @Override
    public void onClick(View view) {

        String name=etName.getText().toString();
        onNameSetListener.setName(name);
        FragmentTwo f2=new FragmentTwo();
        replaceFragment(f2);
    }

    public void replaceFragment(Fragment fragment)
    {
        FragmentTransaction trans=getActivity().getSupportFragmentManager().beginTransaction();
        trans.replace(R.id.rlTwo,fragment);
        trans.addToBackStack(null);
        trans.commit();
    }

    public  interface OnNameSetListener
    {
        public void setName(String name);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            onNameSetListener =(OnNameSetListener)getActivity();

        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(context.toString()+ " must implement OnImageClickListener");
        }


    }
}

This is FragmentTwo

public class FragmentTwo extends Fragment {
    TextView tv;


    public FragmentTwo() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_fragment_two, container, false);
        tv=(TextView) v.findViewById(R.id.tv7);



        return v;
    }

    public void updateName (String name){

        tv.setText("Welcome"+name);

    }

}
Israa
  • 21
  • 1
  • 1
    Possible duplicate of [How to pass values between Fragments](https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) –  Oct 31 '17 at 12:14
  • BTW you forgot to implement your `OnNameSetListener` interface in `FragmentTwo` –  Oct 31 '17 at 12:15

0 Answers0