0

How can I update the editText field with a button when the button and the editText are on different layouts and classes?

I have a mainActivity class and layout but I wand to add this function to an intent (by clicking the save button update main_activity layout). I tried by calling the saveDegrees method onClick in the intent class but that didn't work.

After that I want to go back to the main_activity layout. My saveDegrees code is this:

public void saveDegrees(View view) {
    LayoutInflater inflater = getLayoutInflater();
    View activityView = inflater.inflate(R.layout.activity_main, null);
    mCompassEditText = (EditText) activityView.findViewById(
        R.id.compass_edit_text);
    mCompassEditText.setText(toString().valueOf(currentDegree));
}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Ioanna Dln
  • 309
  • 2
  • 13

1 Answers1

0

You cannot access an activity from another activity. Instead, you should send the data back to the first activity. You can do this by starting the second activity with startActivityForResult() rather than startActivity(). The second activity sets the result before finishing. The first activity receives the result in onActivityResult() and changes its own views. See How to manage `startActivityForResult` on Android? for more details.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268