Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating Views through AsyncTasks
    primarykey
    data
    text
    <p>Below is working sample code of an Android Activity that has 2 buttons, of which only 1 is ever visible: a refresh button and a stop button.</p> <p>This code does the following:</p> <p>Refresh is clicked: start some AsyncTasks. The preExecute of the tasks will hide the refresh button and show the stop button. The postExecute of the task will check if there are no more running tasks and if so, show the refresh button and hide the stop button.</p> <p>Stop is clicked: all tasks are cancelled, the refresh button is shown and the stop button is hidden.</p> <p>This code works fine, but for one exception: when I recreate the activity while a task is running by changing the screen orientation. The buttons will now return to the state as defined in the xml (refresh=visibile, stop=gone).</p> <p>Using a static variable to keep track of the current state of the visibility only makes it worse, because the running Task that has to toggle it back, can only modify views in the calling activity, which has been stopped or destroyed at that point!</p> <pre><code>public class MainActivity extends Activity { private static List&lt;MyAsyncTask&gt; activeTasks = new LinkedList&lt;MyAsyncTask&gt;(); private View refresh; private View stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); this.refresh = findViewById(R.id.refresh); this.refresh.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Start a couple tasks new MyAsyncTask(MainActivity.this).execute(); new MyAsyncTask(MainActivity.this).execute(); } }); this.stop = findViewById(R.id.stop); this.stop.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Cancel all tasks and toggle refresh button cancelAll(); MainActivity.this.enableRefresh(true); } }); } public void enableRefresh(boolean enable) { if (this.refresh != null &amp;&amp; this.stop != null) { this.refresh.setVisibility(enable ? View.VISIBLE : View.GONE); this.stop.setVisibility(!enable ? View.VISIBLE : View.GONE); } } public static void cancelAll() { for (MyAsyncTask task : MainActivity.activeTasks) task.cancel(true); MainActivity.activeTasks = new LinkedList&lt;MyAsyncTask&gt;(); } private class MyAsyncTask extends AsyncTask&lt;Void,Void,Void&gt; { private MainActivity activity; public MyAsyncTask(MainActivity activity) { this.activity = activity; } @Override protected void onPreExecute() { MainActivity.activeTasks.add(this); this.activity.enableRefresh(false); } @Override protected Void doInBackground(Void... v) { try { // Simulate a task Thread.sleep(3000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(Void v) { MainActivity.activeTasks.remove(this); if (MainActivity.activeTasks.size() == 0) this.activity.enableRefresh(true); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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