3

I know there are two ways to set an Activity's orientation to landscape, either programmatically

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

or in the Manifest:

android:orientation="landscape"

Currently, I use the first one in a superclass, because I have many (child) activities that I all want to be always in landscape. However, this make onCreate being called twice, which leads to other issues. When using the Manifest-route, I have to apply it to all activities separately, which will undoubtedly lead to one being missed out in the future (not to mention all the code copying).

Is there a way to apply android:orientation="landscape" to all activities in my app?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

4 Answers4

2

Or you can try make "superclass" for all activities and extends from "superclass".

public abstract class SuperActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Hippo
  • 29
  • 1
1

In the manifest, set this for all your activities:

<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>

Let me explain:

With android:configChanges="orientation" you tell Android that you will be responsible of the changes of orientation. android:screenOrientation="portrait" you set the default orientation mode.

Nitesh
  • 318
  • 3
  • 16
  • 1
    I understand that, but this way I have to do it for each activity separately, which I don't want, because I will eventually forget one. – Bart Friederichs Jun 28 '16 at 12:28
  • you will have to add this in each activity as of now android doesnt provide any feature like that.. – Nitesh Jun 28 '16 at 12:32
0

OnCreate will be called when an activity is created. When you setting in code

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Activity recreates itself, so you can't really stop that. (maybe you can, try it, when you get a solution try posting..)

You gotta handle your stuff that way

Possible Solutions

android:configChanges="keyboardHidden|orientation|screenSize" in manifiest in tag

OR

Add it directly in your manifest the orientation property

johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • Read the question. That is what I posted. I was looking for a way to apply that argument to all activities in the manifest at once. – Bart Friederichs Jun 28 '16 at 18:03
0

I set screen orientation for all activities in my application class.

import android.app.Application
import android.os.Bundle
import android.app.Activity
import android.content.pm.ActivityInfo

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        setListenerToCheckActivitySettings()
    }

    private fun setListenerToCheckActivitySettings() {

        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

                activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }

            override fun onActivityStarted(activity: Activity) {}
            override fun onActivityResumed(activity: Activity) {}
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {}
            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {}
            override fun onActivityDestroyed(activity: Activity) {}
        })
    }
}