Activity restart on rotation Android

前端 未结 30 3837
花落未央
花落未央 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:09

    I just simply added

         android:configChanges="keyboard|keyboardHidden|orientation"
    

    in the manifest file and did not add any onConfigurationChanged method in my activity.

    So every time the keyboard slides out or in nothing happens.

    0 讨论(0)
  • 2020-11-21 05:10

    Put the code below inside your <activity> tag in Manifest.xml:

    android:configChanges="screenLayout|screenSize|orientation"
    
    0 讨论(0)
  • 2020-11-21 05:10
     onConfigurationChanged is called when the screen rotates. 
     (onCreate is no longer called when screen rotates due to manifest, see:  
     android:configChanges)
    

    What part of the manifest tells it "don't call onCreate()"?

    Also, Google's docs say to avoid using android:configChanges (except as a last resort).... But then the alternate methods they suggest all DO use android:configChanges.

    It has been my experience that the emulator ALWAYS calls onCreate() upon rotation.
    But the 1-2 devices that I run the same code on... do not. (Not sure why there would be any difference.)

    0 讨论(0)
  • 2020-11-21 05:11

    You might also consider using the Android platform's way of persisting data across orientation changes: onRetainNonConfigurationInstance() and getLastNonConfigurationInstance().

    This allows you to persist data across configuration changes, such as information you may have gotten from a server fetch or something else that's been computed in onCreate or since, while also allowing Android to re-layout your Activity using the xml file for the orientation now in use.

    See here or here.

    It should be noted that these methods are now deprecated (although still more flexible than handling orientation change yourself as most of the above solutions suggest) with the recommendation that everyone switch to Fragments and instead use setRetainInstance(true) on each Fragment you want to retain.

    0 讨论(0)
  • 2020-11-21 05:12

    Add this line to your manifest :-

    android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
    

    and this snippet to the activity :-

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    
    0 讨论(0)
  • 2020-11-21 05:12

    The way I have found to do this is use the onRestoreInstanceState and the onSaveInstanceState events to save something in the Bundle (even if you dont need any variables saved, just put something in there so the Bundle isn't empty). Then, on the onCreate method, check to see if the Bundle is empty, and if it is, then do the initialization, if not, then do it.

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