I have a program that needs to…
- In
Activity A, do some jobs - Start up
Activity B(aWebView), let user fills in some information, then collect the result - Then finally process the data
Currently I set it up like this:
In Activity A:
...
startActivityForResult(this, new Intent(ActivityB.class));
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
//get result from data, do something with it
...
}
This seems like an awkward approach because I need to split the task into many different parts. I need to handle the exceptions thrown in all parts and it is inconvenient doing it this way. Is there a better method?
Also, after step (3) above, I am going to repeat this step several times, each time posting the final result to a textview. I think that means I need to put them into an AsyncTask, but that makes it even more difficult (where should onActivityResult be put?).
The simple answer is there is no other way. This is how it’s mean to be done in Android. The only thing, I believe, you’re missing is passing a request code to activity B. Without it, you wouldn’t be able to differentiate which other activity returned result to activity A.
If you’re invoking different activities from your A, use different
requestCodeparameter when starting activity. Furthermore, you can pass any data back to activity B using the sameIntentapproach (ok, almost any):Then in your
on ActivityResult:OnActivityResultis executed on the GUI thread, therefore you can make any updates you want straight in here.Finally, in Activity B, you’d have:
I’m not sure why you need
AsyncTaskto handle results.