Forcing activity orientation recreates the activity twice - android

I'm trying to force all the Activities in my app to be either in portrait or landscape mode, exposing a setting to the user.
Basically in the onCreate() I request for the correct orientation somehow like this
#Override
protected void onCreate(final Bundle savedInstanceState) {
setRequestedOrientation(SettingsHelper.getOrientation());
}
In fact what it does is that if we hold the device in portrait mode and the requested orientation is landscape then it simply creates an activity in portrait mode and then it recreates it in landscape. On high end devices this is acceptable since it is instantaneous, however on low end devices it is a disaster since between the creation and recreation there is a 5 second delay...
What I already tried:
- do it in the onResume not only in the onCreate
- define in the manifest android:screenOrientation="nosensor"
- define in the manifest android:screenOrientation="behind"
- define in the manifest android:screenOrientation="unspecified"
- define in the manifest android:configChanges="orientation"
Do you have any idea how to prevent the double creation?

Add this line in your activity tag fo your AmdroidManifest.xml
android:configChanges="keyboardHidden|orientation|screenSize">
Example:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Related

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

Preventing an App from changing to Portrait Orientation when the Device is unlocked

I'm developing a game that runs in a wrapped webview for Android devices. Everything is working as expected--in this case, I have locked my view to landscape orientation, and prevented the device from restarting the activity if the phone is rotated.
However, if I put my device to sleep and then unlock it, I see my application in portrait mode for a moment, until my device re-orients to landscape. This behavior makes sense, since a phone is naturally used in portrait orientation, but I don't want my activity to switch from landscape.
If I start any other game, by which I mean those presumable written in native code, that is locked into landscape mode, and then lock and unlock my phone, the game doesn't do the same quickly-switch-from-portrait-to-landscape dance.
What am I missing?
In MainActivity I have
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
in several places, including onConfigurationChanged, onWindowFocusChanged, onPause, and onResume.
Here is how my activity is defined in my AndroidManifest
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Thanks for any help/advise!
-J.L.
You forgot to remove sensor from the screenOrientation tag. Try this:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:hardwareAccelerated="true"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
the app still shows as the incorrect orientation for a split second when the phone is unlocked.
Did you move setContentView(R.layout.main) to the onResume, AFTER where you put your orientation code? I would try setting the orientation, then the setContentView()

How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets?

I am developing an Android app whose orientation I don't want changed to landscape mode when the user rotates the device. Also, I want the locked orientation to be portrait mode on phones and landscape mode on tablets. Can this be achieved, if yes how? Thanks.
You just have to define the property below inside the activity element in your AndroidManifest.xml file. It will restrict your orientation to portrait.
android:screenOrientation="portrait"
Example:
<activity
android:name="com.example.demo_spinner.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
if you want this to apply to the whole app define the property below inside the application tag like so:
<application>
android:screenOrientation="sensorPortrait"
</application>
Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.
You have to add the android:screenOrientation="portrait" directive in your AndroidManifest.xml. This is to be done in your <activity> tag.
In addition, the Android Developers guide states that :
[...] you should also explicitly declare that your application requires
either portrait or landscape orientation with the
element. For example, <uses-feature android:name="android.hardware.screen.portrait" />.
I can see you have accepted an answer which doesn't solve your problem entirely:
android:screenOrientation="portrait"
This will force your app to be portrait on both phones and tablets.
You can have the app forced in the device's "preferred" orientation by using
android:screenOrientation="nosensor"
This will lead to forcing your app to portrait on most phones phones and landscape on tablets.
There are many phones with keypads which were designed for landscape mode. Forcing your app to portrait can make it almost unusable on such devices. Android is recently migrating to other types of devices as well. It is best to just let the device choose the preferred orientation.
It might be.. you have to identify it is tablet or phone by programmatically...
First check device is phone or tablet
Determine if the device is a smartphone or tablet?
Tablet or Phone - Android
Then......
if(isTablet)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
<activity android:name=".yourActivity"
android:screenOrientation="portrait" ... />
add to main activity and add
android:configChanges="keyboardHidden"
to keep your program from changing mode when keyboard is called.
Set the Screen orientation to portrait in Manifest file under the activity Tag.
Here the example
You need to enter in every Activity
Add The Following Lines in Activity
for portrait
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
for landscape
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"
Here The Example of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.thcb.app">
<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:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Set the Screen orientation to portrait in Manifest file under the activity Tag.
android:screenOrientation="locked"
in <application> for all app
in <activity> for actual activity
I would like to add to Bhavesh's answer. The problem is that if users keep the phone in landscape mode and run the app it will first go to landscape mode since it's based on sensor in manifest and will then immediately switch to portrait mode in phones because of the code in onCreate. To solve this problem below approach worked for me.
1 Declare locked orientation in manifest for activity android:screenOrientation="locked"
<activity
android:name=".overview.OverviewActivity"
android:screenOrientation="locked" />
2 Check for tablet or phone in actiivty or base activity
override fun onCreate(savedInstanceState: Bundle?) {
//check if the device is a tablet or a phone and set the orientation accordingly
handleOrientationConfiguration()
super.onCreate(savedInstanceState)
}
/**
* This function has to be called before anything else in order to inform the system about
* expected orientation configuration based on if it is a phone or a tablet
*/
private fun handleOrientationConfiguration() {
requestedOrientation = if (UIUtils.isTablet(this).not()) {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
} else {
ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
}
UIUtils.kt
fun isTablet(context: Context): Boolean {
return context.resources.configuration.smallestScreenWidthDp >= 600
}
That's it, it will launch the app in the locked mode so that if you are on phone it will be always portraited and if you are on a tablet it will rotate based on the orientation of the device. This way it will eliminate the issue on phones where it switches between landscape and portrait at the start.
Read more here about the locked mode below
https://developer.android.com/guide/topics/manifest/activity-element
Just Add:
android:screenOrientation="portrait"
in "AndroidManifest.xml" :
<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="#string/app_name">
</activity>

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.

How is application orientation (landscape or portrait) locked?

I have tried to freeze orientation using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Although the display stays in portrait orientation, the activity is still recreated. Any ideas how to solve this?
How can the orientation of the application be locked such that the activity is not recreated on orientation change?
First, don't use setRequestedOrientation() if you can avoid it. Use the android:screenOrientation attribute in your <activity> manifest element instead.
Second, you will also need android:configChanges="keyboardHidden|orientation" in your <activity> manifest element to prevent the destroy/recreate cycle.
A more specific example of the activity section of the AndroidManifest.xml for portrait orientation:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where android:screenOrientation sets the initial orientation and android:configChanges voids the events that triggers the corresponding lifecycle methods on screen changes.
Try this:
1.- Set the desired screen orientation in your AndroidManifest.xml
android:screenOrientation="portrait|landscape"
It should look like this:
<application
android:allowBackup="true"
android:icon="~icon path~"
android:label="~name~"
android:supportsRtl="true"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
</application>
2.- Add this to your onCreate void(or wherever you want) in your java Activity File(Example: "MainActivity.java"):
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
It should look like this:
protected void onCreate(Bundle savedInstanceState) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);}
Now the screen wont move even if the Screen Rotation is on in the Device.
The best solution is to use the saved instance.
If you are locking the the screen orientation then it means you are forcing the user to use the app according to constraints set by you. So always use onSaveInstanceState. Read this link: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Categories

Resources