Simple asynchronous web request that does not get lost on activity state change

20 Views Asked by At

I'm trying to figure out how to properly do an asynchronous web request on Android in such a way that when the response comes back, it doesn't get lost if the activity got destroyed and recreated from a state change (for example: a screen rotation) while the request was still in process.

The only work-around that I could think of, was offloading the asynchronous work from the Activity to the Application class (that is, the class referenced from the manifest's <application />), and creating a callback in the Application the Activity subscribes to on onCreate and unsubscribes from on onDestroy.

his works, but seems like a hack to me. What is the proper way to do this?

1

There are 1 best solutions below

0
zen_of_kermit On

Use LiveData and make sure to perform web requests in a background thread.

If you need more specific help, make sure to post relevant code.