java - How to update Activity from thread started in another activity? -
i have main activity , after click on button start thread (but thread hidden in library , have callback in main activity. want start activity (call a) want put results thread. below simplified code:
public class main extends activity { xmanager.resultscallback xresultscallback = new xmanager.resultscallback() { // method called every 10 sec. @override public void onresult(arraylist<string> texts) { } }; xmanager xmanager = new xmanager(xresultscallback); view.onclicklistener onclick = new view.onclicklistener() { @override public void onclick(view arg0) { xmanager.start(); intent = new intent(main.this, a.class); startactivity(i); } }; }
i want update content of activity each time when onresult() method called. how that?
use localbroadcastmanager,
in main activity create function :
private void sendresult() { log.d("sender", "broadcasting message"); intent intent = new intent("custom-event-name"); // can include data. intent.putextra("message", "this result!"); localbroadcastmanager.getinstance(this).sendbroadcast(intent); }
and add broadcastreceiver in a activity
private broadcastreceiver onresult= new broadcastreceiver() { @override public void onreceive(context context, intent intent) { log.d("jazzy","onreceive called"); } };
add on oncreate
@override public void oncreate(bundle savedinstancestate) { // register receive messages. localbroadcastmanager.getinstance(this).registerreceiver(mmessagereceiver, new intentfilter("custom-event-name")); }
add ondestroy
@override protected void ondestroy() { localbroadcastmanager.getinstance(this).unregisterreceiver(mmessagereceiver); super.ondestroy(); }
Comments
Post a Comment