java - Fragment not attached to Activity, in onResume -


i have fragment works fine basically. in onresume method perform api call fresh data when ever user gets activity/fragment.

public void onresume(){     super.onresume();      //in pseudo     async call         success callback           update ui         failure callback           update ui } 

this works fine of time. experience problem when user navigates parent activity, before update ui method has finished. update ui sets text views.

e.g. when user navigates activity , fragment above onresume called. users goes parent activity. async call , update ui isn't finished yet , get:

fatal exception: main process: com.project.foobar, pid: 4405 java.lang.illegalstateexception: fragment mydetailfragment{41ebb478} not attached activity

this thrown in update ui method after success callback. use square's retrofit manage api calls. , again, happens when update ui has not finished. when wait finish , go fine.

any ideas might wrong?

either abort async operation when navigate away fragment, or check see if fragment still attached isadded() when attempt update ui. if it's not attached ignore result , not touch views.

the first option correct hardest implement. second option fine in opinion unless you're doing heavy , long running stuff in background thread.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -