thebadassdev
thebadassdev

Reputation: 166

onDestroy called for Landscape activity A when Portrait activity B is opened from A

I have an Activity A for which orientation (Portrait or Landscape) is set based on some condition. I have another activity B which is Portrait. Now when Activity A is in portrait mode and If I open Activity B then the activity A is not destroyed. But when the Activity A is in landscape mode and If I open Activity B then the Activity A's onDestroy() is called. And when I return back the activity's on create is called. I do not want this to happen. How to not call the onDestroy in activity A when moving from A(Landscape) to B(Portrait).I have attached the manifest below.

I am setting the activity A as landscape using

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE

This is the manifest

<application
    android:name=".FirstChat"
    android:allowBackup="false"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="ABC"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true"
    tools:replace="android:icon,android:label,android:theme">

    <activity
        android:name="SplashActivity"
        android:label="APP"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!-- Branch URI scheme -->
    </activity>

    <activity
        android:name=".ActivityA"
        android:launchMode="singleTop"/>

    <activity
        android:name=".ActivityB"
        android:theme="@style/AppTheme.Transparent"/>
    
</application>

Update on the question:

The issue has been resolved I am putting this here so that if anyone else faces similar issue they can fix it the similar way. when I removed the below code from the manifest for ActivityB. Landscape Activity A stopped recreating itself when moving to Portrait Activity B

android:theme="@style/AppTheme.Transparent"/>

Hence the Landscape Activity A stopped recreating itself when moving to Portrait ActivityB.

The reason was I was requesting landscape mode in activity A programatically and requesting portrait mode in activity B (tried both programatically and manifest orientation). Along with that I had also included the android theme in manifest for Activity B. This combined caused the activity A to destroy and recreate itself once again before going to Activity B. After removing the theme from manifest for Activity B now it works. Now I am setting the theme for ActivityB programatically in code. Everything now works fine.

Note: The above issue happens only when you set a theme in the manifest for activityB and forcefully set different orientation in the activityA and activityB

Upvotes: 0

Views: 97

Answers (0)

Related Questions