简体   繁体   中英

Activity not recreated after device rotation

I am new to android and wanted to solve a really basic problem. I am trying to expecting that my Main activity is recreated and onCreate method is called again when the emulator is rotated but this is not happening. I have added the necessary code in Manifest file.

Here is the code for Manifest file.

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenLayout">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

I am using the following SDK versions

minSdkVersion 24
targetSdkVersion 29
compileSdkVersion 29

Can someone guide why the activity lifecycle methods are not called?

If you want to recreate activity when the device rotates then remove this line: android:configChanges="orientation|screenLayout" from the activity tag of the manifest.

Docs :

android:configChanges

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted.

By declaring

        android:configChanges="orientation|screenLayout"

you are effectively saying that you'll handle orientation changes yourself, and don't want your activity to be recreated.

Just remove orientation (and maybe the entire attribute, unless you do want to handle screenLayout ) to get the default behaviour, which is to recreate the activity.

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