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.
Related
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 :)
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.
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 want to make possible to use an activity in landscape / portrait. The activity starts with the layout for the current orientation (that means before starting the activity). After that it has to stick to it, and not react to orientation change.
I tried putting
android:configChanges="orientation"
in the manifest of the activity, and overriding
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
to do nothing (tried commenting the super call too, but this lead to exception)
but this has not the effect disabling the rotation change - the change is processed and the layout reconstructed, it just doesn't use the correct one.
And I can't use
android:screenOrientation
Because it seems I have to specify only one mode for always, and that's not what I need either.
And anyways, if I specify something there, the activity gets reconstructed when rotating.
Tried with
android:screenOrientation="nosensor"
that doesn't do anything
Here there's a lock of current orientation with code
http://www.samcoles.co.uk/mobile/android-lock-and-unlock-screen-orientation/
But it achieves the same effect as specifying orientation in XML (keeps layout but reconstructs the activity). It's a bit nearer to what I want (keeps orientation from start), but reconstructs the activity and I don't want it to react at all.
android:configChanges="orientation" doesn't work on the emulator at all, but it works fine on devices.
Try
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = getResources().getConfiguration().orientation;
super.onConfigurationChanged(newConfig);
}
Add this to onCreate
if (this.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
else {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}
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.