屏幕旋转前右做点什么? [英] Do something right before Screen Rotation?

本文介绍了屏幕旋转前右做点什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想 do_something()每当设备从纵向旋转为横向

我已经添加了<活动机器人:configChanges =方向> 在我的清单。因此, onConfigurationChanged()每当我转动我的设备将被调用。它会的的重新创建活动。

i have added <activity android:configChanges="orientation" > in my manifest. So onConfigurationChanged() will be called whenever i rotate my device. And it will not re-create the Activity.

在我的 onConfigurationChanged()功能:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        do_something();
    }
    then_fire_off_the_screen_rotation_as_normal();
    // which means re-start the Activity
    // i expect it will fire off onSaveInstanceState() -> onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()
}

我的问题是,我不知道我能在 then_fire_off_the_screen_rotation_as_normal()

我已经使用 setRequestedOrientation()试图,但现在看来,这不是服务于这一目的的功能:

i have tried using setRequestedOrientation() but it seems it is not the function serving this purpose:

当我在 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED 通, ActivityInfo.SCREEN_ORIENTATION_SENSOR ActivityInfo。 SCREEN_ORIENTATION_USER ,该活动仍的的重新启动。 (就像没有叫。)

When i pass in ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED, ActivityInfo.SCREEN_ORIENTATION_SENSOR, or ActivityInfo.SCREEN_ORIENTATION_USER, the Activity is still not re-started. (Just like not calling.)

当我在 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR 通,方向感应器关闭。 (因为它说。)

When i pass in ActivityInfo.SCREEN_ORIENTATION_NOSENSOR, the orientation sensor turned off. (As it said.)

当我在 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE 通,活性为强制关闭 AndroidRuntime:NullPointerException异常

(要具体:这是因为有在纵向布局,但不是在景观布局以编程方式添加片段在 do_something()功能,我会删除这些片段,在风景模式下是不需要的。)

(Be specific: It is because there are programmatically added Fragments in the Portrait layout, but not in the Landscape layout. In the do_something() function, i will remove those Fragments, as they are not needed in Landscape mode.)

推荐答案

唉,我想这种简单的方式可以成为我想做什么:

Well, i guess this simple way can serve what i want to:

不要添加&LT;活动机器人:configChanges =方向&GT; 。意味着没有NEED覆盖 onConfigurationChanged()

Not to add <activity android:configChanges="orientation" >. Means NO NEED OVERRIDE onConfigurationChanged().

的onSaveInstanceState()

@Override
protected void onSaveInstanceState(Bundle outState) {
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        do_something();
    }
    super.onSaveInstanceState(outState);
}

*请确保您 do_something() super.onSaveInstanceState()

(因为在我的情况,我删除片段从布局,并从活动。如果是后 super.onSaveInstanceState(),布局就已经被保存到捆绑,然后将碎片将同时将活动重新创建后重新创建。###)

(Because in my case, i remove the Fragments from the Layout, and from the Activity. If it is after super.onSaveInstanceState(), the Layout will already be saved into the Bundle. Then the Fragments will also be re-created after the Activity re-creates. ###)

###我已经证明了这一现象。但原因<一href=\"http://stackoverflow.com/questions/15555964/what-to-determine-a-fragment-restore-upon-activity-re-create\">What确定一个片段在活动恢复重新创建? 是只是我的猜测。如果您对此有什么想法,请回答<一个href=\"http://stackoverflow.com/questions/15555964/what-to-determine-a-fragment-restore-upon-activity-re-create\">my另一个问题。谢谢!

### I have proved this phenomenon. But the reason of What to determine a Fragment restore upon Activity re-create? is just by my guess. If you have any ideas about it, please answer my another question. Thanks!

好了,上面的方法,它修复了第一个注释提出的问题,进一步完善:(依然简单)

Well, further improvement on the method above, which fixes the problem raised in the first comment: (still simple)

@Override
protected void onSaveInstanceState(Bundle outState) {
    if (isPortrait2Landscape()) {
        do_something();
    }
    super.onSaveInstanceState(outState);
}

private boolean isPortrait2Landscape() {
    return isDevicePortrait() && (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}

isDevicePortrait()会是这样:

private boolean isDevicePortrait() {
    return (findViewById(R.id.A_View_Only_In_Portrait) != null);
}

*请注意,我们的无法使用 getResources()。getConfiguration()。方向来确定设备是的目前字面的肖像。这是因为资源对象改为 右后屏幕旋转 - 甚至在 的onSaveInstanceState()被称为!!

* Notice that we cannot use getResources().getConfiguration().orientation to determine if the device is currently literally Portrait. It is because the Resources object is changed RIGHT AFTER the screen rotates - EVEN BEFORE onSaveInstanceState() is called!!

如果你不想使用 findViewById()测试方向(以任何理由,这不是那么整齐毕竟),保持了一个全局变量私人诠释current_orientation; current_orientation = getResources()初始化它getConfiguration()方向; 的onCreate ()。这似乎更整洁。但是,我们应该知道的活动的生命周期中的任何地方进行更改。

If you do not want to use findViewById() to test orientation (for any reasons, and it's not so neat afterall), keep a global variable private int current_orientation; and initialise it by current_orientation = getResources().getConfiguration().orientation; in onCreate(). This seems neater. But we should be aware not to change it anywhere during the Activity lifecycle.

这篇关于屏幕旋转前右做点什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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