Saving Fragment State on Orientation Change

后端 未结 3 1853
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 20:22

I\'m creating a file manager app for Android, and two classes below are the majority of the logic that goes into doing that. What I\'m doing is that on startup of the Conten

相关标签:
3条回答
  • 2020-12-31 20:55

    use this for save state of fragment on orientation.

    onCreate(Bundle save)
    {
       super.onCreate(save);
       setRetainInstance(true);
    }
    
    0 讨论(0)
  • 2020-12-31 20:56

    this worked for me

    @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            onCreate(savedInstanceState);
        }
    

    and in manifest file inside your Activity tag

    android:configChanges="orientation|screenSize"

    0 讨论(0)
  • 2020-12-31 20:56

    Just posting this since this is a major concerns faced in both work and interviews :) Before API 13 we were using onRetainNonConfigurationInstance() which is deprecated (This method was relate to Activity which is now moved in to fragment)and now we have a new method know as setRetainInstance() (method part of fragment class)this will save the state of fragments even after the activity creation.

    0 讨论(0)
提交回复
热议问题