Search code examples
androidandroid-orientation

Android - What is best way of handling http call in OnCreate which doesn't get called on screen rotation?


I am learning android. Get stuck with a basic scenario of loading the data in oncreate of an activity. So I am trying to load the data as soon as i open my activity but when i change the screen orientation it gets called again. I understand that android restore the state of activity during screen orientation. so oncreate method get called. But my problem is once the data get downloaded i dont want to make an Http call over and over. What should be best possible way to handle this? I want different layout for different screen orientation but data is same. I have to load the data in oncreate.


Solution

  • Retain your data across configuration changes, such as via a ViewModel. Your ViewModel would be responsible for performing the Web request (directly or via a repository) and delivering the results to the activity, such as via LiveData.