Best way to persist data between orientation changes in Android

可紊 提交于 2019-11-29 15:15:39

I will suggest you to fix the orientation from manifest and through program if orientation configuration changes(you can add listener) you can adjust your views as it looks like orientation has been changed. In this way you can save your data as well unnecessary memory storage. Aligning and repositioning views dynamically won't be vague don't worry.

  // Add inside manifest.
  android:configChanges="orientation|keyboardHidden"

  // Reposition your views
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        align_landscape();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        align_portrait();
    }
}

You can also use fragments which can be retained through orientation changes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!