手柄的方向更改状态手柄、方向、状态

2023-09-12 04:40:10 作者:我爱陈奕迅!

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

是这样的:

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

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

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

        super.onConfigurationChanged(CFG);

        LoadState的();
    }
 

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

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

解决方案   

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

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

  

发生时(动画效果)

NBA2K12手柄设置问题

这由OS处理。

  

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

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

  

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

@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.