Activity restart on rotation Android

前端 未结 30 3863
花落未央
花落未央 2020-11-21 04:32

In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is proba

30条回答
  •  梦谈多话
    2020-11-21 05:04

    you need to use the onSavedInstanceState method to store all the value to its parameter is has that is bundle

    @Override
        public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
            super.onSaveInstanceState(outState, outPersistentState);
            outPersistentState.putBoolean("key",value);
        }
    

    and use

    @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            savedInstanceState.getBoolean("key");
        } 
    

    to retrive and set the value to view objects it will handles the screen rotations

提交回复
热议问题