Android screen orientation change have different behaviour on tablet - android

I have activity with:
android:screenOrientation="portrait"
and code:
if(blah blah blah)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
in activity onCreate.
1. When test on smartphone "setRequestedOrientation" causes destroy activity and create again with desired screen orientation.
2. When test on tablet device this code runs only once activity. After "setRequestedOrientation" it just continues without destroy and create again activity. This happens when start code without debuger or without breakpoint in onCreate. If I stop in onCreate function - I see in logs - destroying and onCreate again.
How to catch specific situation with tablet?
UPDATE:
- Problem with tablet occurs when 'Orientation lock' if off. If 'Orientation lock' is enabled program works in same way as in smartphone.
UPDATE2:
- Is it possible to set preffered screen orientation to whole application. So when activity starts to know desired orientation. This will make unnessesary call 'setScreenOrientation' and avoid second activity creation.

After whole day spent with this issue I made following changes in my application:
1. Every activity /without first/ are declared as android:screenOrientation="behind".
2. In first activity I set preferred screen orientation using user selected mode /from my menu.
3. I call setRequestedOrientation only when user changed mode in my menu options.
This causes activities to be created only once. Finaly :)

No.. you cannot set preffered screen orientation for whole application... instead you should call for setScreenOrientation in respective activies only... Refere this link here...

You can set
<activity ActivityName = "test_activity"
android:configChanges="orientation">
</activity>
in your android manifest file. It will not allow activity to restart on orientation change but onconfigurationchange will be called normaly.
Later you can getdisplayorientation in onconfigurationchange and set whatever you want.

Related

activity reload while orientation change in android

I have developed an application and it contains web view.
My issue
When my phone's orientation changes from portrait to landscape the whole application loads again and the web view reloads showing the first page of website.
So I am getting confused about the screen orientation or saving the data during that phase, so how do I fix it...
Yes, in Android the Activity is destroyed and recreated when your change the screen orientation. Thus you need to restore your applications state on recreation. On way is to use the callback method onSaveInstanceState() which you can use to save the state in a Bundle.
The Activities are explained here: http://developer.android.com/guide/components/activities.html and I suggest you take a look on that page for examples and more detailed instructions.
The default behaviour is to restart the activity when a configuration change happens (such as orientation change).
To override this you need to tell the system you'll handle orientation change yourself by adding this to your manifest file in your <activity> element:
android:configChanges="keyboardHidden|orientation|screenSize"
You may also want to override onConfigurationChanged which will be called when such a change happens.
See http://developer.android.com/guide/topics/manifest/activity-element.html#config
The Android Activity lifecycle clearly indicates this behaviour .
What happens
Whenever you start an Activty is gets created(after onStart() method) the
onCreate(Bundle onSavedInstance)
The variable onSavedInstance mentioned above initially recieves null as nothing is saved.
But during the orientation the whole layout hierarchy has to adjust according to the new mode from an existing mode(from portrait->landscape or vice-versa).This change may remove the previous data that you had in your activity.So to avoid such a problem(loosing data), there is a method
onConfigurationChange(Bundle saveSomething)
This method will be called for you to handle some configuration changes into this,
The default implementation will save you some data like some text in an editText.
Note This method as far the specs goes should be used to save some trivial data.
Example
Suppose you had applied a background colour to the activty layout and now you rotated it default implementation won't save it but onConfigurationChange if you want you can save it like this
saveSomething.putInt("color",1);
Inside onCreate
protected void onCreate(Bundle onSavedInstance){
if(onSavedInstance!=null){
int color=onSavedInstance.getInt("color");
if(color==1){
setBackgroundColor(Color.BLACK);
}
}
}
Add the following line inside the activity element of your manifest file you will be handling the changes in configuration
android:configChanges="keyboardHidden|orientation|screenSize"

android orientation - issues with intents (camera)

Here is an interesting one...
I have an application I am writing for devices running android 2.3.3 and above.
It has a main activity which calls the camera via an Intent.
If the user clicks a button to launch the camera; then takes a picture; then clicks "Done" to return to the main activity - the application works fine and displays the new picture in an imageview on the main activity.
however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then click done to return to the main activity - the application crashes
but then...however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then changes the orientation back to portrait (in the camera) before clicking done to return to the main activity - then the application continues to run normally.
Stuff I have tried:
I have changed the manifest file to force the application (main activity) to be oriented in portrait (I have also removed this)
I have added this line to the main activity in an attempt to handle the re-drawing of the activity on it return from the camera intent:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In the debug window I get errors describing nullpointer exceptions - I think this is because the views are no longer there after an orientation change for the image to be passed into unless the orientation is the same as when it was left.
I'm a bit stuck so would appreciate some advice.
When your screen orientation changes, your Activity is destroyed and recreated in the new orientation. So any variables that gained a reference during the life of the Activity will no longer refer to anything, and if you then try to access objects they refer to without re-assigning them values, you'll get a NullPointerException.
The method onSaveInstanceState() is used to save temporary data between configuration changes. This creates a Bundle which is passed to onCreate() when the Activity starts up again.
Without any code, I don't know if that's your problem, but it's worth a look.
See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges for more information (and more accurate information than I've provided, no doubt).
hey i am not sure but try to put this property in manifest file hopefully it should work
android:configChanges="orientation|keyboardHidden"
Some Android devices like Samsung S3 and S4 have default camera surface view in Landscape mode.
When you call camera and capture image and come to your application, whether you had set orientation PORTRAIT it will first open in LANDSCAPE mode and then changes to PORTRAIT mode.
Its Android OS behaviour.
Due to this Activity Re-Creates it self. At that time you are getting Null Pointer Exception.
You can handle by setting configChanges in menifest file.
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
and you can store the data temporary in onSaveInstanceState() rightly said by #Spinner in his answer.

Android: set activity orientation BEFORE onCreate, but not in manifest (HDMI plugged in issue)

I can perfectly manage orientation change programatically, by using setRequestedOrientation in onCreate.
Everything works really fine, until I plug a HDMI cable. After this, the tablet "wants" to be in landscape mode. So when I open an activity, it shows first in "landscape" and then right after it shows in "portrait", (because I'm calling setRequestedOrientation(variable_with_orientation_desired_by_the_user) where variable=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT in onCreate As you can "see", my app keeps rotating on every activity the user opens;
If I set the orientantion for activity in the manifest, this problem is partially gone, since the orientation in manifest is the same that the user has picked up. If they are different, the same behavior starts again.
I tried this, but with no success:
#Override
public void onCreate(final Bundle saved)
{
setRequestedOrientation(ScreenOrientation);
super.onCreate(icicle);
}
So, is there any way to tell to android what orientantion it must create my activity, before it does create it? (But it cannot be in manifest)
I solved it by setting screenOrientation to "behind" in manifest file:
android:screenOrientation="behind"
I solved it using this declaration in manifest:
android:screenOrientation="locked"
on every activity declared in manifest with this issue.
And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,
Hope it helps!

Android - stop move to main activity when rotating the device/emulator

There is an application to load UI dynamically for a unique LinearLayout when button press.
The main UI is extends by Activity and others are extends by LinearLayout.The unique LinearLayout is in main UI.
Dynamically loading part is working smoothly.But when changing orientation of device or emulator it goes to main ui.I have a layout Stack to get current Layout(UI) at any given time.
I want to stop move to main UI when rotating the device/emulator.
Is it possible?
Thanks in advance.
It is probably because the activity is getting recreated on orientation change.
In your manifest file against the main activity add the following properties
<activity android:name=".YourMainActivity" android:configChanges="keyboardHidden|orientation"></activity>
I believew this happens because you don't correctly handle the event of the orientation change. You should override public void onConfigurationChanged (Configuration newConfig)method to handle this.
If you don't override it and don't force your actiivty to keep the orientation, when the orientation changes, the system will destroy your app and re-launch it. That's why you see the main UI again.
Solutions:
explicitly declare the orientation you want in the AndroidManifest.xml file, so such event will be ignored. something like: android:screenOrientation="portrait". See here for more choice.
override the onConfigurationChanged method and chage your UI as you want inside that method, or simply do nothing so that this event would be ignored.

Orientation on android

When ever i change in to another orientation the Activity onCreate() method is called again?
how ever i used
android:configChanges="orientation|keyboardHidden"
but it prevent to change another orientation.how to block on calling the OnCreate() again?
Thanks in advance...
Whenever the orientation changes, the application starts over in the onResume() method.
If you do not want it to change orientation when the user flips the phone, you need to set this in your manifest file:
<activity android:name="YourActivity"
android:screenOrientation="portrait" />
Or landscape if you wish.
If you do want your application to continue where it left of before the user changed orientation on the phone and you do want your application to change orientation too, you need to keep track of the state of the application and set these values in your onResume()-method.
This is a little dirty, but you could simply set a flag-style boolean for the activity. First time through, set the "already ran onCreate" flag to true and use that to avoid running the code multiple times.
Not very proper, but should work.

Categories

Resources