简体   繁体   English

方向更改时,Android活动重新开始

[英]Android activity Restarts When orientation changes

My app seems to restart when it detects an orientation change. 检测到方向改变后,我的应用似乎重新启动。 Well not really. 好吧,不是。

I only have portrait orientation enabled though. 我只启用了纵向方向。

In log cat I can see that my app is still doing calculations, but my progress dialog disappears. 在log cat中,我可以看到我的应用仍在进行计算,但是进度对话框消失了。

What is going on? 到底是怎么回事?

In my manifest file this is my activity. 在清单文件中,这是我的活动。

<activity
            android:name=".xxxActivity"
            android:label="@string/app_name"
            android:configChanges="orientation" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Is there a specific reason why you're listening for the orientation change with android:configChanges="orientation"? 您使用android:configChanges =“ orientation”监听方向变化是否有特定原因?

If not, remove that and I suspect your problem will go away. 如果没有,请删除它,我怀疑您的问题会消失。

add this right after the super call 在超级通话之后添加此权限

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Better to change configChanges to 最好将configChanges更改为

android:configChanges="orientation|keyboardHidden"

I guess, that activity is restarted, because you forgot to add onConfigurationChanged(Configuration) method in your activity. 我猜想,该活动已重新启动,因为您忘记在活动中添加onConfigurationChanged(Configuration)方法。 http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration ) It can be empty but shold be in the activity. http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration )可以为空,但应保留在活动中。

Also android:screenOrientation="portrait" is not needed. 另外,不需要android:screenOrientation="portrait"

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

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