0

I would like to have an appwide rule to not allow the landscape orientation. I realize I could get this by putting:

android:screenOrientation="portrait"

in every one of my activities but that doesn't seem clean. I can also put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

in every one of my activity's onCreate() methods.

But I desire a solution that doesn't require this code duplication. Any suggestions?

aminography
  • 21,986
  • 13
  • 70
  • 74
frillybob
  • 600
  • 2
  • 12
  • 26

1 Answers1

3

Solution:

You can do this for your entire application without having to make all your activities extend a common base class.

The trick is first to make sure you include an Application subclass in your project. In its onCreate(), called when your app first starts up, you register an ActivityLifecycleCallbacks object (API level 14+) to receive notifications of activity lifecycle events.

This gives you the opportunity to execute your own code whenever any activity in your app is started (or stopped, or resumed, or whatever). At this point you can call setRequestedOrientation() on the newly created activity.

class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();  

        // register to be informed of activities starting up
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, 
                                          Bundle savedInstanceState) {

                // new activity created; force its orientation to portrait
                activity.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }

            ....

        });

    }
}

Any doubts, Please leave a comment.

Note: Add in manifest's <application> tag: android:name=".MyApp"

Hope it helps.

enter image description here

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • This makes sense, however I cannot create a `new ActivityLifecycleCallbacks()` because it is NOT abstract and it does not override abstract method `onActrivityDestroyed`. I do not want to override `onActivityDestroyed`. Further, I tried overriding it anyway but I could not override it correctly for some reason. Thanks – frillybob Sep 29 '18 at 20:10
  • 1
    @frillybob I don't understand the meaning of `however I cannot create a new ActivityLifecycleCallbacks() because it is NOT abstract and it does not override abstract method onActrivityDestroyed` .. I created it myself in a new project and it worked fine. – Ümañg ßürmån Sep 29 '18 at 20:13
  • 1
    I've added a screenshot of the solution in my edited answer. @frillybob – Ümañg ßürmån Sep 29 '18 at 20:16
  • The screenshot fixed the error, I didn't know I had to override all of that. However, there is still rotation being allowed in app. I made sure to add `android:name=".MyApp"` inside the `` declaration. There is a warning that `MyApp` is never used in the `MyApp.java` file – frillybob Sep 29 '18 at 20:23
  • 1
    Okay, add this: `activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);` and try. There should be both the lines. @frillybob – Ümañg ßürmån Sep 29 '18 at 20:27
  • Still no luck. I think it has something to do with the class not being used anywhere. Other than the manifest file and the new class (which is alone in its own file) there wasn't anything else I needed to modify correct? The purpose of doing it this way is so that I don't have to extend each each activity right? – frillybob Sep 29 '18 at 20:33
  • 1
    Yes, If you provide a code in a class which extends `Application` class then it will be activated throughout the App. Not sure why this line `activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);` did not work in your app though. But this solution is definitely the global solution of what you are looking for. – Ümañg ßürmån Sep 29 '18 at 20:37
  • 1
    @frillybob Actually the first line itself shouldn't provide you the rotation. No idea what is wrong in the things you are stating. – Ümañg ßürmån Sep 29 '18 at 20:39
  • I am getting a notice in the MyApp class that ` com.###.MyApp` is not registered in the manifest even though I have `` setup – frillybob Sep 29 '18 at 20:44
  • 1
    @frillybob You need to provide this: `android:name=".MyApp"` in `` tag and not in `` tag. – Ümañg ßürmån Sep 29 '18 at 20:48
  • I thought so too, however `.MyApp` will not resolve like it did in the manifest. – frillybob Sep 29 '18 at 20:49
  • 1
    Yeah, Very true. Still can't figure out why are you getting the rotation. I'm not getting in my system though. – Ümañg ßürmån Sep 29 '18 at 20:50
  • 1
    I resolved it. The file that contained `MyApp` was not inside the package that the manifest was referencing. Once I moved it there `.MyApp` was able to resolve correctly in the `` tag. Thanks much for the help! – frillybob Sep 29 '18 at 20:54
  • 1
    Congratulations!! :) @frillybob – Ümañg ßürmån Sep 29 '18 at 20:55