Reputation:
I have two classes in my project: main and importer. Main starts Importer with startActivityForResult(), but how can Importer return its status to Main? Thanks :)
Upvotes: 0
Views: 54
Reputation: 2555
start Activity2 as startActivityForResult(Activity2,REQUEST_CODE)
after you finished your task in the next Activity2
Intent output = new Intent();
output.putExtra("value1", somedata);
setResult(RESULT_OK, output);
finish();
and use onActivityResult() in Activity1
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null)
val1 = data.getIntExtra("value1");
}
Upvotes: 0
Reputation: 157447
When Importer
finishes its job, it has to call setResult(int, Intent)
and the finish()
. Main will receive the result in onActivityResult
. I am assuming that both Importer
and Main
extend Activity
.
Overrid this method inside Main
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
inside importer
Intent data = new Intent();
data.putExtra("result", "i am the result");
setResult(1000, data);
finish();
then
onActivityResult
will receive resultCode = 1000
and data != null
Upvotes: 1
Reputation: 44571
By calling setResult in Importer
when you are ready to return to MainActivity
. You can use setResult(int)
to just send back a result code or setresult(int, Intent)
to send back data.
Then use onActivityResult()
in MainAcitivty
to do what you need with the data
The Activity Docs have an example of doing this, although I don't believe it shows using setResult()
but its pretty straightforward.
Upvotes: 0