2

I have an Activity which records videos from the front camera for the Activity. The orientation is locked to Portrait in the AndroidManifest.xml file via the following code:

<activity
            android:name=".activity.SomeVideoActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="Video Activity"
            android:screenOrientation="portrait" />

This works fine if the user enters the screen for the first time. However, after leaving this activity and coming back to it, apparently landscape mode is re-enabled.

I managed to salvage the situation by forcing the orientation at via the following codeblock:

@Override
 protected void onResume() {
   super.onResume();
   this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

So I am just curious as to why this happens only on just this one activity. I have checked other activities for onActivityResult and they don't messed up the orientation settings at all.

UPDATE:

Actually this problem is solved by using this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); as my sole intention is to lock the activity to portrait and disable landscape rotation. I just want to understand why rotation is re-enabled in SomeVideoActivity after it returns from another activity even though it is declared to have portrait orientation in AndroidManifest.

Rachel Fong
  • 730
  • 1
  • 8
  • 16
  • Remember: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during [onConfigurationChanged().](https://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)) – Emdad Hossain Oct 24 '16 at 08:39
  • @EmdadHossain, I have actually tried removing the `android:configChanges="orientation"` from the manifest previously but it made no difference. In fact, all my activities has the same settings in the manifest and this video recording activity is the one giving rotation issues after returning from another activity. – Rachel Fong Oct 24 '16 at 08:47
  • just to be sure **change** android:configChanges="orientation|keyboardHidden|screenSize" **to** android:configChanges="orientation|screenSize" let me know if any update..!!! – Emdad Hossain Oct 24 '16 at 08:52
  • I did that as well. It did not work. – Rachel Fong Oct 24 '16 at 08:56
  • **change** `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);` **to** setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); – Emdad Hossain Oct 24 '16 at 09:27
  • My intention is to disable landscape and lock screen to Portrait-Only. Wouldn't `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENS‌​OR);` make my screen rotate? – Rachel Fong Oct 24 '16 at 09:30
  • no, as you have defined the orientation in manifest file, but replacing the code does it work? give it a try. – Emdad Hossain Oct 24 '16 at 09:32
  • @EmdadHossain, just tried it. Still rotates. I think I would just stick to my current implementation of `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORT‌​RAIT);` since it is the only way to stop it from rotating. I can only assume that the activity state has been wiped when it returns from the new activity. – Rachel Fong Oct 24 '16 at 09:36
  • OK, after a fair bit of study it shows camera surface view by default goes to landscape mode and see this [answer](http://stackoverflow.com/a/30684890/5689844) to change it to portrait along with other OK configurations – Emdad Hossain Oct 24 '16 at 09:52
  • @EmdadHossain, Thanks for the help. – Rachel Fong Oct 24 '16 at 10:20
  • did it resolve the issue? – Emdad Hossain Oct 24 '16 at 14:59
  • @EmdadHossain, no. Nothing seems to work except for the `setRequestOrientation` line. So I will be sticking to it. Many thanks. – Rachel Fong Oct 25 '16 at 01:36

2 Answers2

1

This post is a bit old, but i had the same problem (or similar). If the device was being held in landscape mode, when finishing the activity, the screen would rotate to landscape and then back to portrait (which is forced in the manifest).

This is the scenario

  • Activity A starts Activity B for result.
  • Activity B does its work and sets result.
  • Activity B finishes itself.
  • Activity A receives the result and finishes itself.

setRequestedOrientation didn't work for me. A small change that i did was instead of finishing the activity A right away after receiving the result, was to delay in 50ms by having a postDelayed call. This worked fine.

new Handler().postDelayed(this::finish, 50);

It is still a bit strange. It seems to be a Android bug when you finish more then one activity with no interval between them. Or just when you are using Camera API, maybe it is a race condition. Anyway, i'm just posting this here in case someone else faces the same problem.

Luis
  • 709
  • 8
  • 10
0

Add in manifest to your activity tag android:screenOrientation="portrait" this will prevent your activity being rotated.

Aniruddh Ambarkar
  • 1,030
  • 1
  • 11
  • 20
  • It is already declared in the manifest. Issue here is that when I return from another activity, landscape mode is re-enabled and the activity is rotatable. – Rachel Fong Oct 24 '16 at 08:55