I have a problem. I have an App, which has a fix orientation (portrait) set in the manifest.xml.
android:screenOrientation="portrait"
But I do want to change the orientation of an Dialog, that I create programmatically in one of my fragments. Problem is, that the super method onConfigurationChanged(Configuration newConfig)is never been called, because of my manifest declaration.
Is it possible to detect that the phone has been turned and change the orientation of the dialog (Without destroying / recreating everything like normally)?
EDIT:
"Activity and Fragments should stay in portrait mode, Dialog should detect changes and orient"
Hope this question is understandable ;)
Thanks!
I think that a viable solution would be to remove the android:screenOrientation tag and then override the onConfigurationChanged() method to do what you need.
Related
I have a DialogFragment in my Android app, and I need to set android:configChanges="orientation|screenSize, I want to load different layout for my DialogFragment in portrait or landscape, but now Android will not automatically do this for me, I think I need to update the layout in onConfigurationChanged , but I don't know how to do it, any sample code will be better, thank you!
Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.
try this code and changed your configuration :
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
This attribute informs to Android system that you are going to handle orientation and screenSize changes for this Activity. So instead of destroying and recreating your Activity, Android will just rotate the screen and invoke one of the lifecycle callback method which is onConfigurationChanged(Configuration).
try this
I finnally find my solution, but I think it maybe only work for my situation, my situation is I want to show a imageView in portrait and not show in landscape, so I set the visibility as visiable or gone in function onConfigurationChanged' and it works
When would one justify adding this to manifest.xml?
android:configChanges="orientation|keyboardHidden|screenSize"
Thankyou
Yes its required based on your requirement. I had an app in which we had to show videos on orientation change the video would restart hence i had to overide the onConfigureation() method and handle it, As i said it depends on your requirement
Adding this to the manifest means that you will control what happens when orientation changes, keyboard is minimized, screen size changes. If you do not add this then the onCreate gets called again on these events.
But say you have a different layout for landscape and portrait. Then you would like to call onCreate again to render the landscape layout.
I am trying to stop Activity recreation due to device orientation change by setting android:configChanges="orientation|screenSize" to my Activity and listening for orientation change by overriding onConfigurationChanged method. I am testing it on Nexus 5, but it is not working in Android L device because onConfigurationChanged never fires. It just sticks to portrait mode, and does not rotate.
Is it a bug in Android L, am I doing something incorrectly, or they have introduced some other way to do this?
Are you usjng android:screenOrientation in your manifest? If so, remove it. This might be silly but, do you have auto rotation enabled?
Aashir is correct! With this, add android:configChanges="orientation|keyboardHidden|keyboard|screenSize" to your activity in menifest.xml
I got an app that is locked in landscape orientation. Now, when I change the devices settings to set its orientation locked to portrait, some problems occur.
After a bit of testing, it seems that for each Activity that is started, the onCreate is called twice.
First time for portrait orientation, second time as an orientationChange with orientation set to landscape. The first orientation is wrong, and probably has something to do with the device being locked to portrait.
In the app, the orientation is forced to landscape programmatically (using setRequestedOrientation), and I prefer to keep it that way, if possible, for various reasons.
Any ideas about how to avoid this behavior?
Thanks in advance!
Ok, I found a solution myself.
After a lot of searching and digging in the code, the solution was fairly simple.
All I had to do was add:
android:configChanges="orientation|screenSize"
... to all my Activities in the manifest file.
This tells the app that I will handle configuration changes myself in the code. Instead of doing this, I didn't add any handlers for these config changes. This way, my Activities aren't recreated when the orientation change happens at the creation of those Activities. I can do it this way, because my app is locked in a certain orientation.
This post came closest to my problem, but it's solution didn't work for me:
Android: set activity orientation BEFORE onCreate, but not in manifest (HDMI plugged in issue)
This post gave me the answer:
http://www.acnenomor.com/1053172p1/oncreate-were-called-twice-after-implementing-setrequestedorientation
Try setting up the orientation directly in the manifest instead of programmatically. Under your activity tag in AndroidManifest.xml add android:screenOrientation="landscape".
This should force the activity to be directly created in the correct orientation.
This is probably a beginner question, but I've already tried looking for an answer and couldn't find one. I was testing an Android app I made on my phone and it was working fine until I switched from portrait to landscape, just to see what would happen. It pretty much made the app go back to the home page, so I'm guessing it calls onCreate() every time the orientation changes. Is it possible to prevent an orientation change from restarting everything, and just have it switch all the views to fit landscape or portrait mode?
This question has been answered thousand times, but ...
You should add in you Manifest.xml for your activity android:configChange="orientation"
and then override onConfigurationChanged(newConfig) method and do not add anything inside, since you are telling you app to do nothing when config changes occur.
Whenever the orientation changes onCreate() method gets called again and again.
Inorder to prevent that add android:configChanges="orientation|keyboardHidden|screenSize"
to the Manifest file, and then override onConfigurationChanged() method.