Answer the question
In order to leave comments, you need to log in
How to properly handle the response from the internet?
Hello! I'm new to android, tell me how to do it right:
I make a request to the Internet (from an activity or a fragment), I get a response, I show it in the window. In crash reports, it is reported that someone is turning the screen, someone goes into a slip, and so on - in general, the response handler tries to render a response to a non-existent activity and crashes.
Accordingly, the question is: how to do all this correctly?
It turns out that you need to separate the request handler from the activity / fragment. But here a lot of questions arise. For example, how should a new activity (after a rotation) determine that a request has already been sent before and is now being executed? Or, for example, the request is completed, but the activity is being recreated at that moment - it turns out that somewhere you need to save the answer until the activity requests it?
How to organize all this correctly? What ready-made libraries are there? Or a good article? And then I don’t get in at all.
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
Use RoboSpice or write your own AsyncTaskLoader based on this article from Ian Lake. I would write my Loader.
You can read about options for handling configuration changes here .
I also advise you to read about architectural patterns for building applications, such as MVP.
As a head-on solution, you can use a singleton object that performs network operations and stores their results, and subscribe to or unsubscribe from this data in the lifecycle methods of your activity or fragment. For example, in onCreate() subscribe to data, and in onDestroy() or onPause() - unsubscribe.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question