UIThread Annotation

After watching Google I/O 2015 - What’s New in Android Development Tools they mention about UiThread. What is UiThread, UiThread is annotation. This is interesting because using these annotation we can reduce some line of to be more cleaner and readble.

So we can transfer from this :

new Asyntask() {
    public void doInBackground(...) { 
        int i = 0;
        while(true) {
            i++;
            updateProgress(textview, i);
        }
    }
}

void updateProgress(final TextView textProgress, final int val) {
  runOnUiThread(new Runnable() {
    @Override
    public void run() {
      textProgress.setText( "" + val );
    }
  });
}

we can specify that updateProgress is can only called by UIThread :

@UiThread
void updateProgress(TextView textProgress, int val) {
  textProgress.setText( "" + val );
}

if you don’t put @UiThread as annotation of updateProgress method, Android Studio is automatically detect that as error and gradle can’t compile it.

You can also use @MainThread difference between @MainThread and @UiThread is @MainThread is care about android life cycle but the @UiThread didn’t. But you can call method @MainThread from @UiThread and vice versa.

Thanks to : Jens Kutson for correcting me.
Great Job Google. Thanks

More info : http://tools.android.com/tech-docs/support-annotations

alt=“IMAGE ALT TEXT HERE” width=“240” height=“180” border=“10” />

 
4
Kudos
 
4
Kudos

Now read this

Achievement of 2014 “The Year of Decision”

Today is the last day of 2014 but this year i face many things and i give tag for this year as “The Year to Train my decision”. I decide to accept an offer from the biggest online travel agent in Indonesia, but unfortunately the deal is... Continue →