Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

java - Go back from another activity to MainActivity

From my MainActivity, I have a button that takes me to a Activity A

now, I have some functions inside Activity A that generates some string data.

What I wanted was, after that string data is generated, I want to go back to MainActivity with some value from Activity A and set that to an EditText.

So I tried this...

In my MainActivity, I have a method that will be called when the method in Activity A that generates that string data is triggered. It looks like this...

MainActivity

public void receiveData(String data) { 
     EditText ed = (EditText) findViewById(R.id.eText);
     ed.setText(data);
 }

Activity A

MainActivity mainActivity;

public void onCreate() {
   ...
   ...
   mainActivity = new MainActivity();
}

public void generateSomeResult() {
   ...
   ...
   mainActivity.receiveData(some_string_result);

   //I tried calling MainActivity in two ways

   //This one causes MainActivity to not fully load maybe why the EditText is not setting the value.
   Intent intent = new Intent(this, MainActivity.this);
   startActivity(intent); 

   //This one causes the MainActivity to fully reload and erased the passed value from Activity A maybe why the value set to EditText has not been set.
   finish(); 
}

Now I'm stuck on what to do. Any idea how to achieve this?

question from:https://stackoverflow.com/questions/65829421/go-back-from-another-activity-to-mainactivity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use startActivityForResult () instead of startActivity() and get your result from Activity A in MainActivity using onActivityResult().

Have a look :

  1. startActivityForResult

  2. onActivityResult


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...