Reputation: 11010
I've constructed a fully work AsyncTask to handle device authorization, but the only catch is I'm having troubling getting the resulting Bundle
back to its instantiation variable to continue processing in the main activity. Here is the AsyncTask instantiation:
Bundle taskResult = new AuthorizeDevice(this).execute(passToAuthroization).get();
I've read some literature on the subject and found that the onPostExecute()
method may have something to do with that, so with that in mind here it is:
protected void onPostExecute(Bundle result){
dialog.dismiss();
Toast.makeText(context, "background process finished: "+result.getString("UDID"), Toast.LENGTH_SHORT).show();
}
So how do I get that bundle back to the main activity?
Upvotes: 1
Views: 568
Reputation: 1002
First, look closer at the API.
I would need to see more of your code to know what you're trying to do, but PravinCG is correct, the way you are using the AsyncTask.execute()
call is incorrect. If you want the return value, get it from onPostExecute()
It looks like you've started with subclassing AsyncTask<?,?,?>
which is the way to go, if you need access to your Activity, could you not simply place the AsyncTask subclass in your Activity class? This would allow you to access your Activity's methods from within onPostExecute(), which occurs, by design, on the UI Thread. (Again, would need to see more code to know how feasible this is, just putting it out there).
if not, simply pass a reference to your Activity into your AsyncTask Subclass and use a public method to return the Bundle from onPostExecute, or use an Interface as has been suggested...
Upvotes: 1
Reputation: 24235
Have a callback interface defined like
public interface AuthorisationCallback() {
void onFinish(Bundle bun);
};
Implement this interface and do whatever you want with the bundle.
AuthorisationCallback mAuthcallback = new AuthorisationCallback() {
void onFinish(Bundle bun) {
// Do what ever with the bundle.
}
};
Send its implementations reference to the AsyncTask
new AuthorizeDevice(mAuthcallback).execute(passToAuthroization);
In onPostexecute() do
protected void onPostExecute(Bundle result){
dialog.dismiss();
mCallback.onFinish(result);
}
Upvotes: 1
Reputation: 13501
As you can see onPostExecute()
method's return type is void. so it does't return anything. so probably you can make Bundle taskResult
a instance variable and initialise it with result
in postExecute
Upvotes: 0
Reputation: 7708
AsyncTask is a non-blocking call and you cannot get the bundle the way you want. However what you can do is to create an interface in AsyncTask which will tell you when it is done. Something like onTaskComplete and you can pass the resulting bundle there.
Upvotes: 2