Updating the UI from a background thread in Android -


edit: suggested helpful add code so, asynctask code pasted below...

i'm learning android , have ui few buttons. want animate ui, changing color of buttons, in sequence.

i shouldn't main thread of course, , doesn't work anyway. code manipulating ui runs ui doesn't update until end of sequence.

so created thread , tried run through sequence background thread however, error trying manipulate ui components background thread. main thread can touch ui components.

then discovered asynctask. figured was, run through sequence in doinbackground(). every time needed update ui i'd call publishprogress() cause onprogressupdate() called main thread access ui components without error.

every time call publishprogress() follow systemclock.sleep(500) let time pass until next animated ui update.

what found though doinbackground() run through 4 ui state changes in 2 seconds (500 ms each) ui not update each call publishprogress(). instead doinbackground() completes , onprogressupdate() called 4 times in row.

from description, publishprogress & onprogressupdate designed update progress bar doinbackground cranks through longish running task so, obviously, onprogressupdate must execute multiple times before doinbackground completes, right?

am missing something?

public class mainactivity extends appcompatactivity {

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main); }  public void startgame(view view) {     mytask task = new mytask();     task.doinbackground(); }   class mytask extends asynctask<void,void,void> {      private int current_int;       @override     protected void doinbackground(void... voids) {         this.current_int = 1;         log.e("doinbackground","light button "+this.current_int);         publishprogress();         systemclock.sleep(500);         this.current_int = 2;         log.e("doinbackground","light button "+this.current_int);         publishprogress();         systemclock.sleep(500);         this.current_int = 1;         log.e("doinbackground","light button "+this.current_int);         publishprogress();         systemclock.sleep(500);         this.current_int = 2;         log.e("doinbackground","light button "+this.current_int);         publishprogress();         systemclock.sleep(500);          return null;     }      @override     protected void onprogressupdate(void... voids) {         super.onprogressupdate(voids);         log.e("onprogressupdate","updating button "+this.current_int);          button btn1 = (button) findviewbyid(r.id.button1);         button btn2 = (button) findviewbyid(r.id.button2);          if (this.current_int==1){             btn1.setbackgroundcolor(getresources().getcolor(android.r.color.holo_blue_light));             btn2.setbackgroundcolor(getresources().getcolor(android.r.color.holo_blue_dark));         } else {             btn2.setbackgroundcolor(getresources().getcolor(android.r.color.holo_blue_light));             btn1.setbackgroundcolor(getresources().getcolor(android.r.color.holo_blue_dark));         }     } } 

}

just reference : asynctask presents systematic way transition main thread (calling thread) new thread (called thread). onpreexecute() , onpostexecute() methods execute on calling thread , doinbackground() actual method executing on new thread. doing ui updates on main thread hence lead exception if done doinbackground() method.

your core background logic should hence placed in doinbackground() method.

if want update ui background thread (asynctask or otherwise), can using :

youractivity.this.runonuithread(new runnable(){     @override     public void run()     {           //ui update operations here     } }); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -