Activity restart on rotation Android

前端 未结 30 3833
花落未央
花落未央 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 04:59

    What you describe is the default behavior. You have to detect and handle these events yourself by adding:

    android:configChanges
    

    to your manifest and then the changes that you want to handle. So for orientation, you would use:

    android:configChanges="orientation"
    

    and for the keyboard being opened or closed you would use:

    android:configChanges="keyboardHidden"
    

    If you want to handle both you can just separate them with the pipe command like:

    android:configChanges="keyboardHidden|orientation"
    

    This will trigger the onConfigurationChanged method in whatever Activity you call. If you override the method you can pass in the new values.

    Hope this helps.

提交回复
热议问题