multithreading - Why does my Android app's view only update under a specific circumstance? -
i working on android app behaving oddly. 1 of first things app upon start up, start listening thread listens "server" app, on same device, data. once data received listening thread, use update main view. this, works if app started after server app.
first few details. app , server app on same device. communicate each other via udp (the server app port of windows application). app uses fragments, view want update not within fragment. code.
content_main.xml
<relativelayout> <linearlayout> <imageview android:id="@+id/my_image_id" android:src="@drawable/my_image" /> </linearlayout> <linearlayout> <!-- fragment code here --> </linearlayout> </relativelayout> mainactivity.java
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate){ listener mylistener = new listener(this); thread listenerthread = new thread(mylistener); listenerthread.setname("my listener thread"); listenerthread.start(); } } listener.java
public class listener implements runnable { public listener (activity mainactivity) {this.mainactivity = mainactivity;} private activity mainactivity; private imageview myimageview; private newdata newdata; @override public void run(){ while(true){ // here app gets data server via udp. // works, getting correct data. myimageview = (imageview) mainactivity.findviewbyid(r.id.my_image_id); mainactivity.runonuithread(new runnable(){ @override public void run(){ if(newdata == 1){ myimageview.setimageresource(r.drawable.new_image_01); } else if(newdata == 2){ myimageview.setimageresource(r.drawable.new_image_02); } else { myimageview.setimageresource(r.drawable.error_image); } } }); } } } like said, if start app after server app, works perfectly. images changes when new data changes. however, if start app before server app, or if restart server app while app still running, images never changes, though still getting correct data server app.
what can make sure view can update @ time?
edit: moved view outside of while loop (as test) , code doesn't work @ all.
i gave try. apparently code working alright, if thread starts after activity been initialized, don't know how server app works tried mimic this.
public class testthreadactivity extends appcompatactivity { private button restart_button; private imageview imageview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_test_thread); toolbar toolbar = (toolbar) findviewbyid(r.id.my_custom_toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); initializeui(); } private void initializeui() { restart_button = (button) findviewbyid(r.id.testthreadactivity_restart_button); imageview = (imageview) findviewbyid(r.id.testthreadactivity_imageview); final listener mylistener = new listener(this); restart_button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview.setimageresource(r.drawable.piccolo); final thread listenerthread = new thread(mylistener); listenerthread.setname("my listener thread"); listenerthread.start(); } }); } public class listener implements runnable { public listener (activity mainactivity) {this.mainactivity = mainactivity;} private activity mainactivity; private imageview myimageview; int newdata = 0; @override public void run(){ while(true){ // here app gets data server via udp. // works, getting correct data. newdata = (new random()).nextint(3); try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } myimageview = (imageview) mainactivity.findviewbyid(r.id.testthreadactivity_imageview); mainactivity.runonuithread(new runnable(){ @override public void run(){ system.out.println("newdata: "+newdata); if(newdata == 1){ myimageview.setimageresource(r.drawable.gohan); } else if(newdata == 2){ myimageview.setimageresource(r.drawable.goku); } else { myimageview.setimageresource(r.drawable.piccolo); } } }); } } } } activity_test_thread.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="activities.list.first.testthreadactivity"> <include layout="@layout/my_custom_toolbar" /> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="4dp" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <button android:id="@+id/testthreadactivity_restart_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:layout_weight="0.5" android:text="start" android:contentdescription="this restart thread" android:textallcaps="false" /> </linearlayout> <imageview android:id="@+id/testthreadactivity_imageview" android:layout_width="match_parent" android:layout_height="200dp" android:src="@drawable/gohan" /> </linearlayout> </linearlayout> 
Comments
Post a Comment