Skip to content Skip to sidebar Skip to footer

Get Result From Async Task In Not Activity Class

I use async task to get data from my database. i have : public class BackgroundDatabaseTask extends AsyncTask { String jsonData; @Override

Solution 1:

An AsyncTask creates a new thread to perform a task that takes a long time. You start this thread by calling execute(). However, you immediately call get() which waits for the task to finish. This completely destroys the whole point of using an AsyncTask in the first place.

Instead, you should remove the call to get() and do the final processing in onPostExecute(). You can do whatever you wish in this method. There is absolutely no requirement that you use the Activity in any way. You can provide data to other classes if that is what you wish.

Solution 2:

You can define an interface in your Asynctask class then implement it where ever you want and get the result from that interface callback

MyTask extendsAsynTask{
publicinterfaceDataListener{
voidonDataReceived(String result);
}
/// then on your onPostExecute method , get an instance of the interface then push the result to the interface method 
dataListener.onDataReceived(result);
}

maybe this will help

Post a Comment for "Get Result From Async Task In Not Activity Class"