Screen Orientation Resets When Navigating Between Activities - android

I have an app which navigates between 2 activities. Both activities initialize the requested orientation to the sensor:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
and I don't have any overrides in the layout files:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainListActivity"
android:background="#drawable/mainbg"
android:scaleType="centerCrop">
and my manifest is the same:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.domain.project">
<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.NoActionBar">
<activity android:name=".OneOfMyActivities"></activity>
<activity
android:name=".MainListActivity"
android:label="#string/title_main_list"
android:theme="#style/AppTheme.NoActionBar.NoActionBar" />
<activity
android:name=".DetailActivity"
android:label="#string/title_detail"
android:parentActivityName=".MainListActivity"
android:theme="#style/AppTheme.NoActionBar.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="my.domain.project.MainListActivity" />
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The app functions correctly as far as updating orientation when you rotate the device. If you take the device and rotate it into landscape mode, the screen correctly swaps to landscape, and stays there. However, when you navigate to the other activity in the app, the other activity displays in portrait mode, and then immediately shows the rotate animation before going to landscape. So, it obviously knows which orientation it should be in. Why does it go from landscape to portrait and back to landscape? Is there any way to have it remember that it's already in landscape, and initialize the new activity in landscape?
Having the screen jump and rotate every time you navigate, when the user is not moving their device is rather annoying.
I've tried googling for this, but all I seem to find are answers about how to lock screen orientation, or questions asking why the activity restarts when the screen orientation changes. Neither of which are my question. I want to still allow the app to change screen orientation when the user rotates the device, I just want it to remember that state when navigating between activities.
thank you.

You can try putting this
android:screenOrientation="fullSensor"
In your activity tag and remove the then redundant
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)

Related

Android Studio - Activity's layout shows flipped (RTL instead of LTR)

I have this issue in my App, which is super weird.
I have an app, when i press Run on Android Studio, it starts the app with the correct layout's direction. Now, i press Back button and go back to the app, it shows a RTL layout instead of the current, correct LTR layout.
The app is pretty simple, it has an Activity with a NavigationButton in the Toolbar, when its flipped, the NavigationButton goes from left to the right, with a false pointing direction.
The app is LTR, supports only English, and tested on an English device, the same result is happening on the Emulator. I have the final version of Android Studio.
Layout, with Toolbar, along with navigation attribute, to enable back arrow.
Activity which setContentView and just shows the layout.
Back pressing, or re-opening the App without clearing it from the BackStack, shows the visual bug.
What cause the issue? as the code is pretty simple and doesn't have anything hardcore.
Manifest Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.corF.app">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:supportsRtl="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:theme="#style/AppTheme">
<activity
android:name="com.corF.app.activities.ActivityStartup"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
A library which i use (SliderLayout), has a code which forces the app to show RTL. On the first launch the SliderLayout forces the RTL, but it doesn't take change until i re-open the app without fully removing it from the backstack.
Silly issue, but yeah, this's the solution.

making steady layout position as from landscape or portrait

how to make the layout of the project app in android studio steady as the position i choose ,i mean how to make landscape or portrait in a steady position and don't change to vise versa as when enter the page the layout go to those positions by it's self and don't be able to be changed even if the device state changed?
< RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inside"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">
In your manifest file you can force the layout of each Activity like this
<activity android:label="#string/app_name"
android:name=".MyActivityName"
android:screenOrientation="portrait" >
/* ... */
</activity>
or
<activity android:label="#string/app_name"
android:name=".MyActivityName"
android:screenOrientation="landscape" >
/* ... */
</activity>

Tablets: android:configChanges="orientation|screenSize" and screen disabling issue

I have application with screen rotation.
rotating mechanism i handle with configChanges:
<activity
android:name=".ui.activity.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"
android:screenOrientation="fullSensor"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Problem:
When user turn off screen, rotate device on 90 degrees and unblock it screen start with "old" orientation of my app, it look ugly.
here scheme:
after block and rotate i get this
blink and app come to normal
try it on Nexus 5(android v5) and Nesus 10(android v4.4), at phones all works fine
Adding android:configChanges="orientation|screenSize" tells the system that you are going to handle these config changes yourself, and for the system to not do anything about it.
By removing them, your activity will be recreated with the correct layouts when you rotate.
If you're using configChanges to avoid the activity being recreated, I feel bad for you son.

setRequestedOrientation doesn't lock the orientation

I am trying to lock an activity to landscape mode and have an overlying activity receive and use the orientation changes. The top activity is partly transparent so it is important that the bottom activity locks and is displayed correctly. I have tried to set it programatically with setRequestedOrientation() and in the AndroidManifest.xml with android:screenOrientation="landscape"
When I lock the orientation using the manifest the top activity also locks. When I add android:configChanges="orientation|screenSize|keyboardHidden" the top activity changes orientation but so does the bottom activity. Sometimes the bottom activity goes completely blank (transparent) after the orientation changes.
This is in my AndroidManifest.xml:
<application
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TopActivity"
android:screenOrientation="sensor"
android:configChanges="orientation|screenSize|keyboardHidden"
android:theme="#style/Theme.Transparent">
</activity>
</application>
MainActivity is the bottom activity. How do I properly handle the orientation changes so that the single activities are locked the way I set them to?
just add screenOrientation to the activity which you don't want to rotate on orientation change in the manifest file like this:
<activity android:name=".Activity"
android:screenOrientation="portrait" />
All other activities will automatically rotate on orientation change.
No need to give screenOrientation for other activities.
1) To disable orientation (i.e. to prevent Android from restarting your activity if the user flips the phone), then specify "configChanges" in your AndroidManifest (like you're already doing, apparently), and then simply don't respond to any changes:
<activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation|screenSize">
2) Alternatively, let's say you want to force your app to "landscape" mode. Instead of "configChanges", you can specify "screenOrientation"
<activity android:screenOrientation="landscape" ... >
3) Finally, if you want to handle the orientation change, you'll need to a) save state, then b) do whatever processing you need (including, perhaps, notify other activities):
http://developer.android.com/guide/topics/resources/runtime-changes.html

Android App Doesn't Stay in Portrait Mode

So I've set my manifest to keep the app in portrait mode, as this is how the game should be played. However, despite doing what I've seen others do, it still switches modes when you tilt the phone. Below is what I've done with the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
android:screenOrientation="portrait">
...
<activity android:name=".GameActivity"
...
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="nosensor">
I've always used
android:screenOrientation="portrait"
in each <activity> to good effect. Give it a shot.

Categories

Resources