Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning same background task from two acitivities
    primarykey
    data
    text
    <p>In my app I have a background task (using AsyncTask) that downloads stuff from a web site.</p> <p>This task can be called from two separate activities, and it is possible to call it twice. So in "activity1" the background task "update" is called, and runs for a while (it takes something like 5-10 seconds usually). Then while it's running, user switches to "activity2" and runs "update" again. This gives problems: either a crash when both try to clear the database (command: DELETE FROM table) at the same time, causing a "database locked" error. Or they try to put the same item in the database causing a duplicate.</p> <p>I've tried to solve this by setting a static boolean flag to true when a task is active. When the task is called, it will check for this flag, and if true (i.e. the same task running on another thread) it goes into a wait loop using a handler until this flag clears, and then returns. This to make sure that when the background task returns, the update has been done. I have to use a Looper for that: this sometimes fails with an error "can create only one looper per thread". And I really have it in a way that only one looper can be started, this is the offending code, which appears at the start of the background task:</p> <pre><code> if (active) { Looper.prepare(); handler = new Handler(); handler.postDelayed(new Runnable() { int count = 0; @Override public void run() { if (active) { count++; if (count &lt; 1000) handler.postDelayed(this, 100); } } }, 100); Looper.loop(); active = false; return "done"; } </code></pre> <p>And to make matters worse it often seems to hang in this loop, without returning.</p> <p>Any ideas on how to solve such a situation?</p>
    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.
 

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