Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several ways of dealing with "multithreadedness" in Android, and your approach may vary accordingly to your necessity.</p> <p>Let's say you just use a simple java thread and have a reference to your activity (like the example provided), you can just call activity.runOnUiThread(Runnable r) and pass in the runnable that is going to update your UI. This actually puts the runnable to be executed after other ui events (like a animation, for example).</p> <p>Another simple way to deal with it is to call "final Handler handler = new Handler()" in your Activity and pass this handler reference to the Thread. The Handler is one special object that is binded with the UI thread of your activity (when called from it) and can provide some methods to also post runnables from a background thread to the ui thread.</p> <p>Yet another way is to use the AsyncTask, that is a simple object that gives you the ability to do something on the ui thread before your processing (like displaying a loading dialog), then do your processing on a background thread and then do something on your main thread again with the results of the background processing. This cicle is onPreExecute() -> doInBackground() -> onPostExecute().</p> <p>There are also other approaches, depending on your needs.</p> <p>Please beware of a few things: </p> <p>Activities have a well defined life cycle, and they may (or may not) die before your background processing happens. You may (or may not) need to define some mechanism to check if the activity is still alive when you try to update its UI or you will get in trouble. AsyncTask have a really useful feature for accepting cancellation based on your activity onDestroy(), that is simply calling "cancel" on the AsyncTask on the activity onDestroy() and checking this cancellation from the AsyncTask.</p> <p>Background threads may (or may not) hold a strong reference to something more then it actually should, depending on your usage. I recommend taking a look at WeakReference from java if you are going to pass references from your activity to your background thread or AsyncTask.</p> <p>Your threading conditions on firing multiple simultaneous AsyncTasks may (or may not) end on a asynchronous or synchronous processing, depending on your platform version.</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.
    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