Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, I would suggest that if a user clicked on something he should see some immediate result, in your case, if the button action needs to wait for something add a circle loading animation on the button immediately to signal to the user the action will happen shortly.</p> <p>Now for the action itself, if there is only one (or few) such possible actions that take place after one (or few) possible background process, I would have the button <strong>synchronously</strong> raise a flag (maybe via a persistent class member) and have the long process <strong>synchronously</strong> check for that flag after it finishes it's process and perform the requested action on finishing.</p> <p>Another possible option if to use wait and notify to have the button code wait for the process to finish, this is usually considered a very high performance solution in Java:</p> <p>for example (ignore typos, this is on-the-spot):</p> <pre><code>button.setOnClickListener(new OnClickListener() { public void onClick(View v) { v.post(new Runnable() { @Override public void run() { synchronized (lockObject) { try { lockObject.wait(); doAction(); } catch (InterruptedException e) {} } } } } } </code></pre> <p>Then have the long process <code>notify</code> when it finishes.</p> <p>Note that <code>new Runnable()</code> doesn't create a new thread, it simply adds a <code>Runnable</code> to the <code>View</code>'s thread queue</p> <p>As suggested by mice, the button onClick handler should check if the process is not currently running, and if so just perform the action immediately.</p> <p>This way you don't need to create a new thread (or an AsyncTask which is actually a pool of threads)</p>
 

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