简体   繁体   English

处理方向变化的状态

[英]Handle states of orientation changes

How can I handle all states of orientation event occurs? 如何处理所有定向事件发生的状态?

Something like: 就像是:

  • before starting (save some screen states) 在开始之前(保存一些屏幕状态)
  • when happening (animation purposes) 发生时(动画目的)
  • after it's happen (load the screen state) 发生之后(加载屏幕状态)

I know that onConfigurationChanged can handle orientation changes. 我知道onConfigurationChanged可以处理方向更改。 And I tried this: 我试过这个:

    public void onConfigurationChanged(Configuration cfg) {
        saveState();

        super.onConfigurationChanged(cfg);

        loadState();
    }

On saveState I store the lastIndex viewed on the Gallery on SharedPreferences. 在saveState上,我存储在SharedPreferences上的Gallery上查看的lastIndex。 On loadState I get the lastIndex from the SharedPreferences and make it as the current on the Gallery. 在loadState上,我从SharedPreferences获取lastIndex并将其作为Gallery上的当前值。

I tried also put loadState in the onResume method but it's not called after the orientation change occurs. 我尝试将loadState放在onResume方法中,但在方向更改发生后不会调用它。

before starting (save some screen states) 在开始之前(保存一些屏幕状态)

Use onSaveInstanceState() and/or onRetainNonConfigurationInstance() and/or a fragment on which you have called setRetainInstance(true) 使用onSaveInstanceState()和/或onRetainNonConfigurationInstance()和/或您调用setRetainInstance(true)的片段setRetainInstance(true)

when happening (animation purposes) 发生时(动画目的)

That is handled by the OS. 这是由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) 使用onRestoreInstanceState()和/或getLastNonConfigurationInstance() (如果你去了片段路由,你的数据自然就在那里)

I know that onConfigurationChanged can handle orientation changes. 我知道onConfigurationChanged可以处理方向更改。

@EightEight's answer covers this nicely. @ EightEight的答案很好地涵盖了这一点。

You may be a little confused. 你可能有点困惑。

This is from the api docs : 这来自api文档

public void onConfigurationChanged (Configuration newConfig) Called by the system when the device configuration changes while your activity is running. public void onConfigurationChanged(Configuration newConfig)当您的活动运行时设备配置发生更改时由系统调用。 Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest. 请注意,只有在清单中选择了要使用configChanges属性处理的配置时,才会调用此方法。

If you've specified the parameter in your AndroidManifest, the system will notify you of orientation change by calling onConfigurationChanged(). 如果您在AndroidManifest中指定了参数,系统将通过调用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() . 如果你没有设置标志,那么Android将终止你的活动开始一个具有正确方向的新活动,然后调用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. 如果您希望在活动完成方向更改并且全部布局后收到通知,我建议在您的一个子视图中覆盖onDraw()方法。 By then everything is done and you could for instance restore the state from before the orientation change. 到那时一切都已完成,例如你可以在方向改变之前恢复状态。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM