android - Accessing fragment method from async task -


first off, practice, accessing fragment's method asynchronous task?

i have async task generates list of latlng use in fragment draw polyline. however, if try use getter method list.

    public list<latlng> getlist() {     return this.list; } 

i nullpointerexceptions have perform inside fragment,

while(list == null) { // fixme delay because of      list = getroute.getlist(); }    

which defeats purpose of having background task.

is there way can call method within async task's post execute method?

    @override protected void onpostexecute(otpresponseui otpresponse) {             fragment.fragmentsmethod(getlist());             mdialog.dismiss();     } 

this way can correctly show process dialog , not leave user hanging while loads list.

update tried invoking callback this callback function in fragment not executed.

update2 ok passed fragment instance async task able call fragment method. per advice:

create list object in custom asynctask class, , return fragment in postexecute() method. can directly calling method on fragment instance (that you'll either via constructor. works, thanks!

you have few options:

define own custom asynctask class , pass list want filled either constructor:

class myasynctask extends asynctask<void,void,void> {     private list<latlng> mthelist;      public myasynctask(list<latlng> thelist) {         mthelist = thelist;     }      // fill list in doinbackground()      ... }  // in fragment  myasynctask task = new myasynctask(thelist); task.execute(); 

or can pass parameter execute() method:

class myasynctask extends asynctask<list<latlng>,void,void> {     public void doinbackgroun(list<latlng>...args {         list<latlng> thelist = args[0];         // fill list     } } 

note can pass fragment instance execute() method in same way , call getlist() method on instance (i don't option).

a better option be:

create list object in custom asynctask class, , return fragment in postexecute() method. can directly calling method on fragment instance (that you'll either via constructor or argument execute() method) accepts list parameter. but, (cleaner) way define interface within custom asynctask class callback method accepts filled list argument. fragment can implement callback interface, add task "listener", , have task call inteface method, passing filled list agrument inside task's postexecute() method.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -