Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing fragment method from async task
    text
    copied!<p>First off, is this a good practice, accessing a fragment's method from an asynchronous task?</p> <p>I have an async task which generates a list of LatLng which is use in my fragment to draw a polyline. However, if i try to use a getter method to get the list.</p> <pre><code> public List&lt;LatLng&gt; getList() { return this.list; } </code></pre> <p>I get <code>nullpointerexceptions</code> so i have to perform this inside the fragment,</p> <pre><code>while(list == null) { // FIXME delay because of this list = getRoute.getList(); } </code></pre> <p>Which defeats the purpose of having a background task.</p> <p>Is there a way I can call the method from within the async task's post execute method?</p> <pre><code> @Override protected void onPostExecute(OTPResponseUI otpResponse) { Fragment.fragmentsMethod(getList()); mDialog.dismiss(); } </code></pre> <p>This way i can correctly show the process dialog and not leave the user hanging while it loads the list.</p> <p><strong>UPDATE</strong> I tried invoking a callback like <a href="http://blog.evoxmusic.fr/content/how-implement-callback-asynctask" rel="nofollow">this</a> but the callback function in my fragment is not executed.</p> <p><strong>UPDATE2</strong> Ok i just passed the fragment instance to the async task to be able to call the fragment method. As per your advice:</p> <blockquote> <p>Create the list object in your custom AsyncTask class, and then return it back to the Fragment in the postExecute() method. You can do this by directly calling a method on the Fragment instance (that you'll get either via the constructor. It works, thanks!</p> </blockquote>
 

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