Access Fragment Method From Asynctask Postexecute
I need some help to manage fragments and asynctask, I´ve search a lot in google and here in stackoverflow but I didn´t get a positive result. I have an app with a TabsActivity an
Solution 1:
Try passing your fragment to the AsyncTask like this:
classWorkerTaskextendsAsyncTask<Void, Void, Void> {
privateMyMapFragment mapFragment;
WorkerTask(MyMapFragment mapFragment) {
this.mapFragment = mapFragment;
}
@OverrideprotectedVoiddoInBackground(Void... params)
{
...
}
@OverrideprotectedvoidonPostExecute(Void res)
{
mapFragment.updateStuff(...);
}
}
Solution 2:
I had the same problem. It got solved using a broadcastreceiver. This is how I did it:
classTabsTaskextendsAsyncTask<String, Void, String> {
static final StringBROADCAST_ACTION = "CALL_FUNCTION";
@OverrideprotectedStringdoInBackground(String... params) {
//some statements
}
protectedvoidonPostExecute(String result) {
try {
Intent broadcast = newIntent();
broadcast.setAction(BROADCAST_ACTION);
mContext.sendBroadcast(broadcast);
} catch (Exception e) {
//some statement(s)
}
}
}
Now, in your fragment class:
privateBroadcastReceiver receiver = newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
function_you_want_to_call();
}
};
publicvoidonResume() {
//other statementsIntentFilter filter = newIntentFilter();
filter.addAction(TabsTask.BROADCAST_ACTION);
activity.registerReceiver(receiver, filter);
}
publicvoidonPause() {
//other statements
activity.unregisterReceiver(receiver);
}
Post a Comment for "Access Fragment Method From Asynctask Postexecute"