How to handle orientation change easily - android

I'm working on a android application that adds every view programmaticaly.
When the user turns the screen, I just want to values that are filled in to be displayed again.
Is there an easy way to let Android do this automaticaly?
My application is completely dynamic so it doens't have a predetermined layout, which makes it a lot harder.
So, how can I easily save the state of my screen?

Everytime orientation change, android create new view and destroy the old one. You can saved your data when orientation change and re-initialize when the new view is created
Use onConfigurationChanged method of activity to detect Orientation Change
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
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();
}
}
Don't forget to edit the appropriate element in your AndroidManifest.XML like this to include the android:configChanges
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">

For an expanded explanation of Hein's correct answer see my previous post:
https://stackoverflow.com/a/57239493/5696328

Related

How to prevent restarting from orientation change?

Here is the structure of my application.
MainActivity.java calls FragmentActivity.java,
FragmentActivity.java calls GameView.java
GameView.java calls Thread.java.
Basically all the gaming logic will be handled by GameView and its thread.
I don't know how to prevent from restarting the game when there is a orientation change.
If i paused the thread and resume it, the app crashes and also i can not use onSaveInstanceState method in Gameview.java
Any help?
It is completely possible through the Android manifest.
Just add in the activity declaration, in which you want to disable the restarting the following attribute:
android:configChanges="orientation|screenSize"
Then you can overwrite the onConfigurationChanged() in your activity and you get the callback which event just happened. In your case the orientation change. And with this approach the activity doesn't restart, when your orientation changes.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
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();
}
}
You are looking for onResume() and on pause() ... You store values into a bundle in pause and instantiate those values on the resume method and show a dialog as this happens to prevent the user from noticing the obvious.
You can have some control over what happens on orientation change, by defining an Application class in your manifest file, and overriding public method onConfigurationChanged().
Base class for those who need to maintain global application state.
You can provide your own implementation by specifying its name in your
AndroidManifest.xml's tag, which will cause that class
to be instantiated for you when the process for your
application/package is created.
You just need to check for newConfig == Configuration.ORIENTATION_LANDSCAPE and such. At this point you might want to reload resources to get things working.

Rotation restarts activity

I have a problem with my android app. My activities restart when I rotate my phone (onCreate is called). I have googlet and have tried
android:configChanges="keyboardHidden|orientation"> in my manifes, but with no luck. Can someone please explain to me how I can get this to work
EDIT: I find out that i nedd to ad these codes
manifest
<activity
android:name="?"
android:label="#string/?"
android:theme="#style/?"
android:configChanges="orientation|screenSize">
MainActivity
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
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();
}
}
In your Activity, you can override the method onSaveInstanceState. You can save the information you need into a Bundle. That bundle will be passed in to the onCreate method after the orientation has changed. If you would rather just specify one orientation to be used, within your manifest file, either under the application tab or activity tag, place:
android:screenOrientation="portrait"
You can try android:configChanges="keyboard|orientation|screenSize">. However, be aware that restarting the Activity on configuration change is normal behavior. Unless you have very specific needs, you should not be using configChanges.

Change screen orientation in Android without reloading the activity

I want to change the orientation programmatically while running my Android App, with these lines of code:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
They work so far, but the main problem is that the whole activity is reloaded when the screen orientation changes, and I don't want that. Is it possible? Thanks.
EDIT: OK, after I while I found out what I was missing. I had to include also "screenSize" in the configChanges property, so having
android:configChanges="orientation|screenSize"
solved the whole thing.
See the edit by #luisfer. For targeting Android 3.2 and above, you need BOTH
android:configChanges="orientation|screenSize"
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter
In AndroidManifest file add android:configChanges="orientation" for the activity you want to handle this orientation
In activity use onConfigurationChange overrided method. Do task you want to handle in orientation change.
Ansewered here:
Android, how to not destroy the activity when I rotate the device?
Add:
android:configChanges="orientation"
To your androidmanifest.
see:
http://developer.android.com/guide/topics/manifest/activity-element.html#config
Call this method And Set manifest file
android:configChanges="orientation|screenSize|keyboardHidden"
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
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();
}
}

Show different layout for both orientations

In my android application i designed two different layouts with same file name say my_profile.xml, and stored in two different directories i.e, 1) res/layout, 2) res/layout-land. Now the problem is if i start activity in Portrait mode it loads Portrait mode layout but after changing orientation it doesn't change layout, But if i start activity in Landscape mode it loads layout of landscape i.e, perfect. Problem is only when i change orientation, it doesn't handle it automatically. Can any one tell me, what can be the problem?
Check for the following :
1) In manifest file check for the below line in your activity
android:configChanges="orientation|keyboardHidden|screenSize"
2) Override the below function
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag,"onconfig");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// do something
Log.d(tag,"land");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// do something
}
}
-Preeya
In your manifest.xml make the changes like this,,,,,
<activity android:name=".myActivity" android:windowSoftInputMode="adjustPan" android:configChanges="keyboardHidden"></activity>
dont use configuartionChanges="orientation"
Thats it...
Check your manifest file. If you have the following in your activity then REMOVE IT:
android:configChanges="keyboard|orientation|screenSize"
orientation causes the the activity to resort to the same layout rather than creating a new one.

Views still rotate when OnConfigurationChanged is overridden

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.

Categories

Resources