Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid AsyncTask with notification
    text
    copied!<p>I am currently starting to develop Android applications, and I must say that it all came out very very simple and straightforward.</p> <p>I have a small question about the AsyncTask. Maybe I've been doing something wrong, but here's the situation.</p> <p>I have a small app that needs to load a list's content from the web. I developed everything based on fake requests, and it all came out awesome. Then I updated the code with actual requests and got the 'Network on main thread error'. So I decided to switch to AsyncTask. </p> <p>I was wondering if I could let AsyncTask just do the asynchronous work, and handle the result somewhere else (where I actually have the GUI connections and everything). I thought that in terms of readability and logic it makes more sense to have all the code that handles the interface in the Activity, but how could I let the Activity know when a task was completed?</p> <p>I wrote these simple classes and interfaces (and it works) but I wanted to know from you if this is a good thing or there are better methods to do that.</p> <p>So, here's the code:</p> <pre><code>public interface AsyncDelegate { public void executionFinished(LazyLoaderWithDelegate lazyLoaderWithDelegate); } </code></pre> <p>This is a simple interface. The purpose is to have the Activity implement this and handle the 'executionFinished' method. Something like a listener.</p> <pre><code>import android.os.AsyncTask; public class LazyLoaderWithDelegate&lt;Params, Progress, Result&gt; extends AsyncTask&lt;Params, Progress, Result&gt;{ AsyncDelegate delegate; Result result; public LazyLoaderWithDelegate(AsyncDelegate delegate){ this.delegate = delegate; } @Override protected Result doInBackground(Object... params) { //This will be Overridden again from the subclasses anyway. return null; } @Override protected void onPostExecute(Result r){ this.result = r; delegate.executionFinished(this); } public Result getResult(){ return result; } } </code></pre> <p>This class basically gives a skeleton structure to notify the delegate when the task is finished.</p> <p>That's all. Here's an example of using this classes:</p> <pre><code>public class LazyVideoLoader extends LazyLoaderWithDelegate&lt;Void, Void, List&lt;List&lt;Video&gt;&gt;&gt;{ public LazyVideoLoader(AsyncDelegate delegate) { super(delegate); } @Override protected List&lt;Video&gt; doInBackground(Void ...params) { return ServerInterface.getVideos(); } } public class MainActivity extends Activity implements AsyncDelegate { private LazyVideoLoader videoLoader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * Set up the lazy loaders */ videoLoader = new LazyVideoLoader(this); videoLoader.execute(); } @Override public void executionFinished(LazyLoaderWithDelegate task) { if(task == videoLoader){ List&lt;Video&gt; result = videoLoader.getResult(); //Do whatever you need... } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload