手柄的方向更改状态 [英] Handle states of orientation changes

查看:103
本文介绍了手柄的方向更改状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何处理的取向事件发生时,所有的状态?

是这样的:

  • 在开始(节省一些屏幕状态)
  • 在发生时(动画效果)
  • 在它的发生(加载屏幕状态)

我知道 onConfigurationChanged 可以处理的方向变化。我试过这样的:

 公共无效onConfigurationChanged(配置CFG){
        saveState和();

        super.onConfigurationChanged(CFG);

        LoadState的();
    }
 

在saveState和我存储lastIndex的观察上共享preferences画廊。 在LoadState的我得到的lastIndex的从共享preferences并使其作为画廊的电流。

我试着也把LoadState的在onResume方法,但出现的方向变化后,它不叫。

解决方案
  

开始(节省一些屏幕状态)之前

使用的onSaveInstanceState()和/或 onRetainNonConfigurationInstance()和/或在其上呼吁片段 setRetainInstance(真)

  

发生时(动画效果)

这由OS处理。

  

在它的发生(加载屏幕状态)

使用 onRestoreInstanceState()和/或 getLastNonConfigurationInstance()(如果你去的片段路线,您的数据很自然仍然存在)

  

我知道onConfigurationChanged可以处理方向改变。

<一个href="http://stackoverflow.com/questions/7842691/handle-states-of-orientation-changes/7842788#7842788">@EightEight's回答涵盖这很好。

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. 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. On loadState I get the lastIndex from the SharedPreferences and make it as the current on the Gallery.

I tried also put loadState in the onResume method but it's not called after the orientation change occurs.

解决方案

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.

这篇关于手柄的方向更改状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆