Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Easy:</p> <ol> <li><p>Create <code>interface</code> class, where <code>String output</code> is optional, or can be whatever variables you want to return. </p> <pre><code>public interface AsyncResponse { void processFinish(String output); } </code></pre></li> <li><p>Go to your <code>AsyncTask</code> class, and declare interface <code>AsyncResponse</code> as a field :</p> <pre><code>public class MyAsyncTask extends AsyncTask&lt;Void, Void, String&gt; { public AsyncResponse delegate = null; @Override protected void onPostExecute(String result) { delegate.processFinish(result); } } </code></pre></li> <li><p>In your main Activity you need to <code>implements</code> interface <code>AsyncResponse</code>.</p> <pre><code>public class MainActivity implements AsyncResponse{ MyAsyncTask asyncTask =new MyAsyncTask(); @Override public void onCreate(Bundle savedInstanceState) { //this to set delegate/listener back to this class asyncTask.delegate = this; //execute the async task asyncTask.execute(); } //this override the implemented method from asyncTask @Override void processFinish(String output){ //Here you will receive the result fired from async class //of onPostExecute(result) method. } } </code></pre></li> </ol> <hr> <p>UPDATE</p> <p>I didn't know this is such a favourite to many of you. So here's the simple and convenience way to use <code>interface</code>.</p> <p>still using same <code>interface</code>. FYI, you may combine this into <code>AsyncTask</code> class.</p> <p>in <code>AsyncTask</code> class :</p> <pre><code>public class MyAsyncTask extends AsyncTask&lt;Void, Void, String&gt; { // you may separate this or combined to caller class. public interface AsyncResponse { void processFinish(String output); } public AsyncResponse delegate = null; public MyAsyncTask(AsyncResponse delegate){ this.delegate = delegate; } @Override protected void onPostExecute(String result) { delegate.processFinish(result); } } </code></pre> <p>do this in your <code>Activity</code> class</p> <pre><code>public class MainActivity extends Activity { MyAsyncTask asyncTask = new MyAsyncTask(new AsyncResponse(){ @Override void processFinish(String output){ //Here you will receive the result fired from async class //of onPostExecute(result) method. } }).execute(); } </code></pre> <p>Or, implementing the interface on the Activity again </p> <pre><code>public class MainActivity extends Activity implements AsyncResponse{ @Override public void onCreate(Bundle savedInstanceState) { //execute the async task new MyAsyncTask(this).execute(); } //this override the implemented method from AsyncResponse @Override void processFinish(String output){ //Here you will receive the result fired from async class //of onPostExecute(result) method. } } </code></pre> <p>As you can see 2 solutions above, the first and third one, it needs to create method <code>processFinish</code>, the other one, the method is inside the caller parameter. The third is more neat because there is no nested anonymous class. Hope this helps</p> <p><strong>Tip</strong>: Change <code>String output</code>, <code>String response</code>, and <code>String result</code> to different matching types in order to get different objects. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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