Best way to persist data between orientation changes in Android

前端 未结 2 1650
借酒劲吻你
借酒劲吻你 2020-12-21 07:17

My education application is having a tab host with 6 to 7 tabs with landscape and portrait modes support. In each and every activity associated with a tab, I am showing some

2条回答
  •  既然无缘
    2020-12-21 07:58

    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();
        }
    }
    

提交回复
热议问题