-1

Hello i have fragment inside of Activity. I have two Buttons one for fragment and activity. The job of button in fragment is get the value of edittext and the job of button on activity is get the value from button and set it to textview. This is my approach. MainActivity Button that will get the value and set it to MainActivity textview

  public void onClick(View view){
      Intent i = getIntent();

      String name = getIntent().getStringExtra("name");

      //SET DATA TO TEXTVIEWS
      nameTxt.setText(name);
  }

first (Fragment)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_first, container, false);
    nameFragTxt= (EditText) root.findViewById(R.id.m1);

    Button cxz = (Button)root.findViewById(R.id.ccc);
    cxz.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(getActivity(), Main2Activity.class);
            i.putExtra("name",nameFragTxt.getText().toString());
        }
    });

    return root;
}

Using startActivity(i); is working but this is not i want. What do you guys think is the best approach to do this.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    Possible duplicate of [Communicating between a fragment and an activity - best practices](https://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices) – ADM Jul 22 '18 at 14:18

1 Answers1

0

You can use Bundle too.

 Bundle bundle = new Bundle(); //Add your data from getFactualResults method to bundle
 bundle.putString("VALUE_NAME", value);//Add the bundle to the intent
 i.putExtras(bundle); 
 startActivity(i);
urvi joshi
  • 158
  • 13