Save data and change orientation

后端 未结 6 758
礼貌的吻别
礼貌的吻别 2020-11-27 23:16

I have two activities and I use android:configChanges=\"keyboardHidden|orientation|screenSize\"

 @Override
      public void onConfigurationChan         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 00:18

    You can save any Object by Overriding public Object onRetainNonConfigurationInstance () and calling getLastNonConfigurationInstance() in your onCreate method.

    @Override
        public Object onRetainNonConfigurationInstance() {
    
    
        return data;
        }
    
    
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
             data = getLastNonConfigurationInstance();
        }
    

    but if you do this, you have to change your manifest and code, so the normal process for a configuartion change is used.

    Different from the SavedInstance method, this only saves the object if the activity is killed because of a configuaration change

提交回复
热议问题