i am getting strange issue in my app. The main issue that i have asked here. issue is android:configChanges="keyboardHidden|orientation" is not working in my code.
so i have found the solution to manage it by #Override onConfigurationChanged() method in my code to manage orientation. but yet the issue is not solve properly.
Currently issue is that onConfigurationChanged() is calling twice when we change orientation
landscape to portrait.
If we change phone portrait to landscape its changing and working but now when user move the phone landscape to portrait then onConfigurationChanged() will call and return same orientation state & in second call it will return portrait.
Code :
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.e("On Config Change", "LANDSCAPE");
Toast.makeText(getApplicationContext(), "L", Toast.LENGTH_LONG)
.show();
} else
{
Log.e("On Config Change", "PORTRAIT");
Toast.makeText(getApplicationContext(), "P", Toast.LENGTH_LONG)
.show();
}
}
Log
first mode its port mode , so change in land mode
02-28 12:10:06.274: E/On Config Change(540): LANDSCAPE
02-28 12:10:14.154: E/On Config Change(540): LANDSCAPE
// here after changed the land mode try to chage in port mode then its calling two times as you can see as per the log
02-28 12:10:14.593: E/On Config Change(540): PORTRAIT
02-28 12:11:39.524: E/On Config Change(540): LANDSCAPE
One more query with same question >>
It will kill the current activity when we change the orientation (at time of calling onConfigurationChanged). so i have two layouts in different folder as per my previous question.So when i change the screen activity will remove all data.so how can i save that data to show user when user change the phone orientation in any case.
I am not sure how onConfigurationChanged() gets called twice, but to avoid buggy behavior in your app you can void the execution of onConfigurationChanged() if the orientation received now is same as the previous orientation.
Worked for me, hope it works for you too :)
Related
I'm creating an app, which requires landscape / portrait screen. My problem is, when screen is rotate , the method doesn't work even the logger , so i'm having a hard time trouble shooting things. Anyone who have experience same? What i received in logs after rotation is
12-28 00:02:55.897 13039-13039/com.xxx.xxx D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-28 00:02:55.927 13039-13039/com.xxx.xxx D/ViewRootImpl: ViewPostImeInputStage processPointer 1
In here, i'm trying to load a different XML when user is in landscape or portrait. I'm using this method because, i have additional controls when in landscape mode, and those controls are not available during portrait mode. so i need to call bindNewControlsLoaded() method to initialize those controls.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_task_page_lan);
initCreate();
//im calling this because some controls are newly added
bindNewControlsLoaded();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_task_page);
initCreate();
}
}
initCreate method is a method for portrait , this is use to load common controls. while additional controls are called in the bindNewControlsLoaded();
In my manifest android:configChanges="orientation|screenSize"
By default android recreate activity on rotate so you can do it without onConfigurationChanged at all just use resource qualifiers . In your case just move activity_task_page_lan.xml to res/layout-land/ folder and rename to activity_task_page.xml.
But if you really want to handle this case with your code and prevent activity recreation you should add android:configChanges="orientation" to activity declaration.
For my current activity I currently have the following added to the manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
and in he actual activity I have overridden the OnConfigurationChanged method as follows:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
The views in the activity still rotate, but I want to specify that some of them should not rotate.
Does anyone know how to avoid the inevitable rotation of all the UI elements within an activity?
The caveat here is that I still need the OS to open the keyboard in landscape mode when the keyboard becomes activated - I just don't want the other views rotating. I believe this means that forcing the activity to be in portrait mode (which I know how to do) will not work unless someone knows how to specifically open the softkeyboard in landscape mode while the activity is in portrait mode.
Thank you in advance.
I am making an App which allow you read Wikipedia pages.
I want to display an icon each time when is phone is rotated from the portrait to landscape or vice versa. It will let user lock the screen orientation if he wants to or else the screen is oriented according to the sensor data. This is functionality is implemented by some of the App in the google play, for example - Pocket
To do this is i have overridden the
public void onConfigurationChanged(Configuration newConfig)
Now if i am setting the locked configuration by using( orientation_dir is the orientation value stored in the shared preferences and it is correct as i have debugged through the code for it.)
if(orientation_dir == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else if(orientation_dir == Configuration.ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
then orientation is set correctly but then onConfigurationChanged() method is not called when the phone is rotated.
If i set the orientation this way
if(orientation_dir == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(orientation_dir == Configuration.ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
then desired orientation is not set. Phone reset the orientation according to the sensor data.
I tried even by not calling the super in the method as i thought it might be setting it wrong but then it gives me the exception "Super not called".
I am trying for last 2 days and haven't got a any solution to this problem.
In general onConfigurationChanged() event fire only when an configuration change has occured. In your app the orientation changed event occurs only when the screen is free to rotate. If you have locked the screen orientation then the screen orientation event is not fired. onConfigurationChanged() does not listen to the sensor that is responsible to rotate the device but fires only when the appropriate event is happening.
So what you really want is to have access to a SensorManager and attach a SensorListener. This is the way for you to listen to the actual orientation of the device.
Here is a very nice demo that demonstrates the SensorManager capabilities with the orientation of the phone:
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
UPDATE: The orientation sensor is a composite sensor to make things easier for the developer. It does not actually exist in the phone. Is a very neat sensor combining the accelometer sensor and the magnetic field sensor. And the OrientationSensor is currently deprecetated according to the docs (http://developer.android.com/guide/topics/sensors/sensors_position.html).
See What is the alternative to android orientation sensor? for sample usage.
it may need some fixing I have not tested it much.
onConfigurationChanged - is a notification to let your activity know the change in orientation.
You should not attempt to change or fix the orientation in that method. If you set the orientation to landscape or portrait then you will not get new orientation change notification because the orientation of the activity does not change. May be you should set the orientation setting in the event handler of the widget that you plan to show when there is a change in orientation.
Welcome all
I have a special need for my app. I must force the app to work only in portrait mode, but i need to know when the user has moved the phone to landscape mode. Why? because i am displaying a opengl view with a texture image, and when the user changues the phone position to landscape mode i must rotate the polygon without reseting the activity. Then i must force portrait mode on manifest, because i dont want that my onCreate method gets called again.
Please, can someone tell me how to achieve this?
I know how to rotate the image, i only need to know when the user has moved the phone to landscape position, but with portrait forced on manifest.
I tryed adding android:screenOrientation="portrait" in the manifest and also adding android:configChanges="keyboardHidden|orientation" to the declaration in the manifest; and then overriding onConfigurationChanged() in my activity. But this doens't works, because portrait mode is forzed, then onConfigurationChanged method is never called......
Thanks
Why not use the SensorManager to monitor when the phone has rotated 90 degrees. Actually this may be helpful also.
use this attribute in manifest android:configChanges="orientation" this stops recreating activity then override the onConfigurationChange() method. Give the same layout both in portrait and landscape mode. when orientation change occurs, that method wil be called then change the image as u like
Do Not set the screen orientation in the manifest (android:screenOrientation="portrait"). Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.
Here a example of your onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); // tem que ter
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
}
I need to apply different layouts for portrait and landscape orientations of my activity. Besides, I need to show alert if orientation is portrait.
I have specified android:configChanges="orientation|keyboardHidden" in AndroidManifest. I also override onConfigurationChanged method like this:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
....
}
While rotating from landscape to portrait log looks like:
config changed
Portrait
But while changing from portrait to landscape it looks like
config changed
Portrait
config changed
Landscape
Why onConfigurationChanged is called twice? How can I avoid it?
See my answer to another question here: https://stackoverflow.com/a/3252547/338479
In short, handling configuration changes correctly is hard to do. It's best to implement onRetainNonConfigurationInstance() which is called just before your application is about to be stopped and restarted due to a configuration change. Use this method to save anything you want ('this' is a good choice) and then let the system tear down your app.
When your app gets restarted with the new configuration, use getLastNonConfigurationInstance() to retrieve the state you just saved, and use it to continue your application without all that mucking about with bundles and shared preferences.
You can simply save the previous orientation and check if it has really changed.
If you set in AndroidManifest.xml android:configChanges to keyboardHidden|orientation for your activity, onCreate etc... won't be called. That makes the implementation significantly easier to implement. But of course layout will change from portrait to landscape as well.
Is there any particular reason you chose to handle rotation in this manner? While it is quicker since the activity doesn't get restarted on an orientation change, it isn't typically recommended, if I recall correctly. Another way to handle orientation changes is instead of overriding onConfigurationChanged(), overriding onCreate(), onStart() or onResume() such that
#Override
public void onStart() {
super.onStart();
int orientation = getWindowManager().getDefaultDisplay().getOrientation();
if(orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i(TAG, "Orientation is portrait");
// show whatever alerts here
}
}
and then specifying two layouts - one for portrait, one for landscape. The portrait version of the layout would remain at res/layout/whatever.xml, and the landscape version would live in res/layout-land/whatever.xml. The AndroidGuys had written a bunch of good articles on this topic, see http://androidguys.com/?s=rotational+forces&x=9&y=9
I'm pretty sure you would want to use onCreate rather than onStart. The only difference appears to be that onStart will get called when the application comes to the foreground. That wouldn't be a case where you'd want to make the user wait for you to re-initialize the UI. Otherwise, just change your setContentView call based on that if condition.
Android starts a new instance of your activity when changing orientation so using onCreate is the ideal method. You will have to save/restore your activity's data obviously in order to pick up where you left off - but you should be doing this anyway since any number of events can unfocus/kill your application.