简体   繁体   中英

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.

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"?

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

android:configChanges="orientation|keyboardHidden"

I guess, that activity is restarted, because you forgot to add onConfigurationChanged(Configuration) method in your activity. http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration ) It can be empty but shold be in the activity.

Also android:screenOrientation="portrait" is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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