Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Learning to think in terms of events is indeed the key here. You can do it. :)</p> <p>The first rule is: never stall the UI thread. The UI thread is responsible for keeping your app feeling responsive. Any work you do there should not block; do what you need to do and return as quickly as possible. Definitely avoid doing I/O on the UI thread. (There are some places where you can't really help it due to lifecycle requirements, for example saving app state in <code>onPause</code>.) If you <strong>ever</strong> call <code>Thread.sleep</code> on the UI thread you are doing it wrong.</p> <p>Android enforces this with the "Application not responding" (or "ANR") error that the user sees. Whenever you see this in an Android app it means the developer did something that caused the UI thread to stall for too long. If the device is really bogged down for some reason this error might not actually be the app developer's fault, but usually it means the app is doing something wrong.</p> <p>You can use this model to your advantage by posting your own events. This gives you an easy way to tell your app, "do this later." In Android the key to posting your own events is in the <a href="http://developer.android.com/reference/android/os/Handler.html" rel="noreferrer"><code>Handler</code></a> class. The method <a href="http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,%20long)" rel="noreferrer"><code>postDelayed</code></a> lets you schedule a <a href="http://developer.android.com/reference/java/lang/Runnable.html" rel="noreferrer"><code>Runnable</code></a> that will be executed after a certain number of milliseconds.</p> <p>If you have an Activity that looks something like this:</p> <pre><code>public class MyActivity extends Activity { private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler.postDelayed(new Runnable() { public void run() { doStuff(); } }, 5000); } private void doStuff() { Toast.makeText(this, "Delayed Toast!", Toast.LENGTH_SHORT).show(); } } </code></pre> <p>Then 5 seconds after the activity is created you will see the toast created in <code>doStuff</code>.</p> <p>If you're writing a custom <code>View</code> it's even easier. Views have their own <a href="http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable,%20long)" rel="noreferrer"><code>postDelayed</code></a> method that will get everything posted to the correct <code>Handler</code> and you don't need to create your own.</p> <p>The second rule is: Views should <strong>only</strong> be modified on the UI thread. Those exceptions you're getting and ignoring mean something went wrong and if you ignore them your app will probably start misbehaving in interesting ways. If your app does most of its work in other threads you can <a href="http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)" rel="noreferrer"><code>post</code></a> events directly to the view you want to modify so that the modifications will run correctly.</p> <p>If you have a reference to your <code>Activity</code> from that part of your code you can also use <a href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)" rel="noreferrer"><code>Activity#runOnUIThread</code></a>, which does exactly what the name implies. You might prefer this approach if posting to a single view doesn't really make sense in context.</p> <p>As for updates to views not appearing until you hit a button, what kind of views are these? Are they custom views that are drawing these updates? If so, are you remembering to call <a href="http://developer.android.com/reference/android/view/View.html#invalidate()" rel="noreferrer"><code>invalidate</code></a> after data changes to trigger the redraw? Views only redraw themselves after they have been invalidated.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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