Android: prevent the screen orientation from changing, but still get a notification about it.

I need to save my action layout in portrait mode, but still get notified when the screen orientation changes to update my own custom canvas. I managed to prevent the Android OS from reloading my activity and changing the layout when I change the orientation of the screen by putting these lines in my XML manifest:

android:screenOrientation="portrait" android:configChanges="orientation" 

I also know that I can change the screen orientation programmatically by calling the setRequestedOrientation method from Activity.

The problem is that the onConfigurationChanged method does not call at all when I set the screenOrientation flag or call the setRequestedOrientation method. It makes a call when I remove the flag from the manifest, but then the layout is also updated, and that is not what I want.

Is there a way to prevent changing the OS in the layouts, but can I still catch the configuration change?

Thanks!

+4
source share
2 answers

I hope this class solves your problem.

+4
source

I had the same problem. The thing that helped me was to add one word to my mainfest XML file:

keyboardHidden

It should look like this:

 android:configChanges="orientation|keyboardHidden" 

For example, in my manifest file:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pl.chris.mypicture" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MyPictureActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
+1
source

Source: https://habr.com/ru/post/1398485/


All Articles