java - When asynctask in activity finishes how to reload ui in fragment? -


my activity loads fragment , starts asynctask.

 class getparkingareasasync extends asynctask<string, string, string> {       @override     protected string doinbackground(string... args) {          return mrepository.getparkingareas(mcontext.getactiveuser());     }      @override     protected void onpostexecute(string value) {         (int i=0; i<mcontext.getactiveuser().getparkingareas().size(); i++){             log.i(tag_areas, "parking area " + +": " + mcontext.getactiveuser().getparkingareas().get(i).getname());         }     } } 

this asynctask in activity loads data (i store in context). when finishes how can reload ui in fragment show data loaded (in parent activity)?

the onpostexecute runs in uithread, need fragment call activity. way defining interface fragment. this:

public interface yourfragmentlistener {     void onparksloaded(list<string> names) } 

then on activity:

public class youractivity implements yourfragmentlistener {      @override     void onparksloaded(list<string names) {         // names         yourfield.settext(names.get(0))     }  } 

finally in fragment can use this:

@override protected void onpostexecute(string value) {     ((yourfragmentlistener) getactivity()).onparksloaded(value) } 

of course can change types better fit use case. beware of cast, if use fragment in activity not implements yourfragmentlistener throw exception


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -