Android - Activity states newbie question - android

I am trying to make a quiz app in which the questions are all displayed on the same activity. Everytime the screen is rotated, on create method is recalled and the quiz returns back to its first question, losing all the data regarding the other questions.
How can I prevent this ?
Thanks.
*ps: I don't want my application to work only at portrait or landscape mode but at both.

In your AndroidManifest, within your activity add:
android:configChanges="orientation"
So something like:
<activity android:name="MyActivity"
android:configChanges="orientation" />
This will tell the app that you want to handle changes by orientation yourself. You can do nothing with it, or catch the corresponding method in your app.

You can add this to your activities entry:
android:configChanges="keyboardHidden|orientation"

Related

Can a live wallpaper lock the screen in portrait mode?

Can a live wallpaper lock the screen in portrait mode? And if so, how?
I should mention that I have seen two supposed answers to this question on SO, one appeared inordinately complicated, I didn't understand it all and the answer was not accepted by the original poster. The second answer didn't work for me.
A third answer involving the use of:
android:screenOrientatin = "portrait" or "landscape"
has been suggested, but it is not clear exactly where this should go in the manifest.
EDIT: have tried putting android:screenOrientation="portrait" in many different places in the manifest, and none of them worked.
EDIT: another answer was to rotate your bitmaps and handle a rotation by just drawing everything sideways - but this looks very ugly because, as you rotate you phone, the OS instigates a rotation animation - which means that you get a horrid jumping effect as you turn the phone.
I'm beginning to suspect that the true answer is simply "no".
Did you try setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ?
In your AndroidManifest.xml you should have something like:
<application
android:icon="#drawable/app_icon"
android:label="#string/nuboLogin"
android:name=".LoginApplication"
android:debuggable="true">
<activity
android:name=".WallPaperActivity"
android:label="#string/wallPaper"
android:windowSoftInputMode="adjustPan"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This should make sure that your Activity runs in portrait mode. If you prefer landscape, you can easily guess what you should modify
Android application restarts the activity when the orientation changes. You can either use
android:configChanges in your manifest. The activity is shut down and restarted by default, when a configuration change occurs at runtime, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
use android:screenOrientatin = "portrait" or "landscape" it will force the app to run in the mode you specify. However it will not prevent the activity from being shut down and restarted.

Fix screen orientation to portrait

I know this might sound as a thousand times answered question but I am really confused because this should be so easy that I don't understand what's going on.
I want one of my activities shows only in portrait mode. I thought, ok, just write in the AndroidManifest.xml something like this:
<activity android:name="Control" android:screenOrientation="nosensor">
</activity>
And that's all.
But the app ignores it. It switches to landscape when I turn the device. I am using ActionBarSherlock, Fragments and ViewPager.
How can I fix the orientation of the Activity? Thank you very much in advance.
we fix like this
<activity android:name=".Control" android:screenOrientation="portrait">
</activity>
your Activity name Must Be
android:name=".Control"
Have you tried android:screenOrientation="portrait" ?
Add android:screenOrientation="portrait" in your manifest.
OK, so this was another of my typical Logic errors.
If you have a Fragment with a ViewPager that contains another three Fragments it doesn't matter if you try to change the behaviour of any of the embeded Fragments because it won't work, logically. If you set screenOrientation to portrait on the main Fragment with the ViewPager that contains the other you will, now yes, prevent app from changing orientation.

Prevent app from restarting during orientation change

I have a canvas, and it is redrawn when orientation is changed. It's a custom canvas not android provided.
When screen layout is changed, application state and (all of it's view's states) gets reset. I changed screen orientation to portrait only; it keeps screen layout unchanged but application is again resetted.
I checked documentation and found that activity is destroyed and restarted again when orientation change occurs. Savestate() cannot save and load bitmap data or any large data which are required by my custom canvas.
I again checked documentation, and found Handling run time changes topic, which mentioned onConfigurationChanged() which gets called when specific configuration change is occurred, which in my case is 'orientation'. This method prevents restart and leaves to developer how that configuration change should be implemented. It even mentioned in last paragraph that if I will not implement that method then this will just cause the activity to skip onRestart() and will do nothing. I am setting manifest file as
android:screenOrientation="portrait"
android:configChanges="orientation"
And I am not implementing onConfigurationChanged(). But this doesn't help either. I don't know why. It seemed so helpful to me.
Please post solution if you have any.
Also, app takes some resonable time, and I would like app do not restart when orientation is changed. Actually I don't want to do anything when this happens.
I am using emulator too, so please clarify if it is emulator only problem.
P.S. My internet connection is down and I am using my stupid mobile client.
I have checked the offline documentation. And please bear with me for spellings. I am trying to find solution, but currently I am stumpped.
Use this in you AndroidMenifest.xml
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboard|keyboardHidden"
android:screenOrientation="sensor" />
write this code in manifest:
android:configChanges="orientation|screenSize|keyboardHidden"
For each activity in the AndroidManifest, the screenOrientation can be specified. For example, to specify that the activity always stays in portrait mode, the following can be
added to the activity element:
android:screenOrientation="portrait"
Similarly, landscape mode can be specified using the following:
android:screenOrientation="landscape"
However, the previous still causes the activity to be destroyed and restarted when a hard
keyboard is slid out.Therefore, a third method is possible:Tell the Android system that the
application should handle orientation and keyboard slide-out events.This is done by
adding the following attribute to the activity element:
android:configChanges="orientation|keyboardHidden"

Android options menu closed every time screen orientation changes

For some reason, every time the screen orientation is changed my app closes the context menu if it was open. All applications I have seen keep menus open after rotating the screen, but I cannot figure out why my app is closing them.
I am not handling configuration changes on my own and my onCreate method, which I know is called after every orientation change, does not touch menus at all. I would appreciate any insights on this problem.
The contextMenu disappeared because by default when rotating android calls destroy() and then onCreate() but :
If you don't want Android to go through the normal activity destroy-and-recreate process; instead, you want to handle recreating the views yourself, you can use the android:configChanges attributes on the element in AndroidManifest.xml.
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden">
</activity>
This way contextMenu is not closed when phone rotates, because onCreate() method is not called.
This topic can also be helpful - Activity restart on rotation Android

Android force Horizontal (landscape) layout [duplicate]

This question already has answers here:
Force "portrait" orientation mode
(12 answers)
Closed 2 years ago.
I'm pretty close to finished with my first game for Android, and I've come across a problem that's so simple I'm sure I'll feel stupid for not knowing how to solve it, but how can I force the app to stay in a Horizontal layout? Right now, if you turn the phone (emulator) it flips the graphics and squeezes them. I want the game to start horizontally and stay that way regardless of how the user turns the phone.
Thank you.
Open the AndroidManifest.xml and add the following android:screenOrientation="landscape"
e.g.
<activity android:name=".ActivtyName"
android:screenOrientation="landscape"
>
To do this at the Activity level, in case you want to change it dynamically or allow the user to select the orientation, use setRequestedOrientation in your Activity's onCreate method.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
etc...
In the AndroidManifest file, try add android:screenOrientation="landscape" to the activity's attribute.
e.g.
<activity android:name=".myMainActivity"
android:screenOrientation="landscape">
Or, you could use a R.styleable file and set the Orientation settings there. More info
I think you should be able to set
android:screenOrientation="landscape"
on your Activity in the manifest. It doesn't look like there's an Application wide setting, so you'll probably need to do it for each Activity separately.
Here is an easier way might help someone having same problem and dont want to deal with xml. You can go to Application Nodes select your activity and select screen orientation. See the image attached for clarity.

Categories

Resources