When using Retrofit (http://square.github.io/retrofit/) when you make an API call you need to use a Callback to deal with the response :
https://gist.github.com/lawloretienne/f3c5944985cf686be8ff
However, I am wondering how to do with these two scenarios
1) Make an API call from Fragment A. Navigation to Fragment B before then response comes back. Then the response comes back at some later time while Fragment B is visible.
2) Launch the app with the Screen display turned off, where Fragment A is the first visible Fragment in the app.
The issue with scenario 1) is that if you try to access any of the views to update the UI, a NullPointerException will be thrown. The way I have avoided this is by putting an isAdded() && isResumed() as the first line of the onResponse() and onFailure() callback methods. But if you do this check, then when the display is off and you first launch the app, then the logic which would update the UI in Fragment A never runs since the isResumed() method returns false.
What is the best way to handle both scenarios?
Working with the Retrofit 2.0 Callback | Heykuki News