Search code examples
androidretrofitscreen-rotation

Retrofit asyncronous calls and screen rotation


I was wondering what is the correct way to handle screen rotations while waiting for an asynchronous callback. The callback tries to touch some UI elements on the activity, but as it gets destroyed I get a null pointer exception.


Solution

  • I'm not sure how the logic of your code it set up, but I wouldn't have the callback methods directly touch the UI elements. Instead, have them touch your Model, and when the onCreate method in your Activity is called, you then take the data out of the Model. That way you don't have to rely on the UI elements to be there - you just rely on the Model.

    Hope this was helpful - if not, let me know.