首页 > 解决方案 > 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.

标签: androidandroid-orientation

解决方案


跨配置更改保留您的数据,例如通过ViewModel. 您ViewModel将负责执行 Web 请求(直接或通过存储库)并将结果传递给活动,例如通过LiveData.


推荐阅读