Execute action on AsyncTask finish
<p><strong>SITUATION</strong></p>

<p>I am developing and android app for my university math class (I'm using Android Studio). What I am doing in a particular fragment is the following:</p>

<ol>
<li>Click the button "GENERATE"</li>
<li>Start the AsyncTask (there is a "big" for loop inside, it goes from 0 to 5000)</li>
<li>While the AsyncTask is running show a message like "Loading..."</li>
<li>When the AsyncTask has finished the calculations inside that foor loop, add TableRows to a TableLayout</li>
</ol>

<p>As you can see here, I must execute that <code>for</code> loop in a "separate" place (the AsyncTask) because it is going to be a problem for the UI.</p>

<hr>

<p><strong>PROBLEM</strong></p>

<p>I have troubles when I have to code the point number 4 of the above list. You can see the entire code in <a href="http://pastebin.com/FsJGHvj0" rel="nofollow">this pastebin</a>. Here I want to point out the main problem.</p>

<pre><code>    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment_disequazioni_generali, container, false);

        Button button = (Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                dialog = ProgressDialog.show(view.getContext(), "", "Attendi...", false, true);

                //valori degli estremi
                EditText val_a = (EditText) view.findViewById(R.id.val_a);
                EditText val_b = (EditText) view.findViewById(R.id.val_b);
                a = Double.parseDouble(val_a.getText().toString());
                b = Double.parseDouble(val_b.getText().toString());
                derivata = getDerivative(funzione, variabili, "x");

                //setup tableview + task
                tl = (TableLayout) view.findViewById(R.id.tableLayoutTangenti);
                TextView coefficente = new TextView(view.getContext());
                EditText decimale = new EditText(view.getContext());

                mAuthTask = new TaskAsincrono();
                mAuthTask.execute((Void) null);

            }

        });
</code></pre>

<p>As you can see here I am creating a new object because I need to use the AsyncTask (look below): </p>

<pre><code>public class TaskAsincrono  extends AsyncTask&lt;Void, Void, Boolean&gt; {
        @Override
        protected void onPreExecute(){
        }

        @Override
        protected void onProgressUpdate(Void[] values) {

        };

        @Override
        protected Boolean doInBackground(Void... params) {

                double x = a;
                double y = b;

                //Definizione dell'intervallo
                risultati = separaRadici(funzione, x, y, 500);

                int k = 0;

                for (int i = 0; i &lt; (risultati.size()); i += 2) {

                    k++;
                    double a = risultati.get(i);
                    double b = risultati.get(i + 1);
                    double res = (b + a) / 2;

                    for (int j = 1; j &lt; 10; j++) {
                        res = res - (getFunctionVal(funzione, res) / getFunctionVal(derivata, res));
                    }

                    soluzioni.add(res);

                }

            return true;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if(dialog.isShowing()) { dialog.dismiss(); }

            //THE ISSUE IS HERE
            mostraRisultati();
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
        }
    }
</code></pre>

<p>When the AsyncTask finished to work ( = it has completed all the operations inside the doInBackground) I need to call <code>mostraRisultati();</code>.</p>

<p>When the AsyncTask finished to execute the for loop, the dialog disappears but the method <code>mostraRisultati()</code> is not working. Any idea?</p>

<p><strong>Note:</strong> If you look at the pastebin you can see the entire code. While testing, I have discovered that mostraRisultati() is called when the AsyncTask has finished its process, but the <code>for</code> loop is not running because <code>risultati.size()</code> is zero. What am I missing?</p>