RapsFan1981
RapsFan1981

Reputation: 1197

Refresh text EditText after finish()

I created a 'New File' activity using

startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);

The NewFile activity lets users set certain options for their text file then after clicking a button a string is saved to a static variable in my StringBuilder class and finish(); is called. How can I load this new string into the MainActivity's EditText? onCreate() is only called when the activity is first created right?

Upvotes: 0

Views: 184

Answers (3)

Houcine
Houcine

Reputation: 24181

in your class NewFile.java :

String strName = "toto";
Intent intent = new Intent();
intent.putExtra("name", "toto");
setResult(1, intent);
finish();

in your MainActivity.java :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 1) {
      if (resultCode == RESULT_OK) {
          // Handle successful result
          String name  = intent.getStringExtra("name");
          editText.setText(name);
      }
    }
}

refer this tutorial for more explanations

Upvotes: 0

Ariel Magbanua
Ariel Magbanua

Reputation: 3123

Do it on onResume or onActivityResult. It would be ideal though onActivityResult since you've used startActivityForResult, before finishing the other activity you set the setResult(int resultCode, Intent data) if you have intent to sent back or if none setResult(int resultCode). I think it is better to put the string which will be used to update your EditText as extra in the intent, then set the text using that string in onActivityResult method.

Upvotes: 1

RapsFan1981
RapsFan1981

Reputation: 1197

@Override
    protected void onResume() {
        super.onResume();
        et.setText(DocumentBuilder.docText);
    }

Upvotes: 0

Related Questions