Handle states of orientation changes

后端 未结 2 1693
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 20:36

How can I handle all states of orientation event occurs?

Something like:

  • before starting (save some screen states)
  • when happening (animation
相关标签:
2条回答
  • 2021-01-06 21:13

    before starting (save some screen states)

    Use onSaveInstanceState() and/or onRetainNonConfigurationInstance() and/or a fragment on which you have called setRetainInstance(true)

    when happening (animation purposes)

    That is handled by the OS.

    after it's happen (load the screen state)

    Use onRestoreInstanceState() and/or getLastNonConfigurationInstance() (if you went the fragment route, your data is just naturally still there)

    I know that onConfigurationChanged can handle orientation changes.

    @EightEight's answer covers this nicely.

    0 讨论(0)
  • 2021-01-06 21:17

    You may be a little confused.

    This is from the api docs:

    public void onConfigurationChanged (Configuration newConfig) Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest.

    If you've specified the parameter in your AndroidManifest, the system will notify you of orientation change by calling onConfigurationChanged(). If you did not set the flag, then Android will kill your activity start a new one with a proper orientation and then call onResume().

    If you want to be notified when activity finished orientation changed and is all laid out, I would recommend overriding an onDraw() method in one of your child views. By then everything is done and you could for instance restore the state from before the orientation change.

    HTH.

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