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.
Related
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.
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();
}
}
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'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
I'm interested to know which methods are overridden, when an Android device is rotated (i.e. when configuration changes)?
onSaveInstanceState(...), onConfigurationChanged(...), onRestoreInstanceState(...) - something similar to this?
It will be also interesting for me to listen about the whole process connected with changing a configuration. Thanks.
According to the android developer reference you have to use:
#Override
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();
}
}
For more information see: http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
You could be interested in seeing: http://developer.android.com/guide/topics/manifest/activity-element.html#config
Here you can find all configuration listener you can listen.
Remember to put them in the android manifest:
<activity
android:configChanges=["orientation"]
. . .
</activity>
Hope this helps...
When you rotate the device your Activity will re-create and all the variables will be re-initialized. So, in that case if you want to some values to remain same on Rotation also you can store their state using the onSaveInstanceState() and you can restore in onCreate() again by checking Bundle is not null.
if(savedInstanceState != null){
// get the restore value from the Bundle
}
Where as onConfigurationChanged() will be called when you rotate the Device(Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest). From the parameter you will get the new device configuration.
If you don't want your Activity to get re-created on rotation of Device then you have to add this line to your activity tag in the AndroidManifest file.
android:ConfigChanges="keyboardHidden|orientation"
From android developers...
To retain an Object during a runtime configuration change:
Override the onRetainNonConfigurationInstance() method to return the Object you would like to retain.
When your Activity is created again, call getLastNonConfigurationInstance() to recover your Object.
Android calls onRetainNonConfigurationInstance() between onStop() and onDestroy() when it shuts down your Activity due to a configuration change. In your implementation of onRetainNonConfigurationInstance(), you can return any Object that you need in order to efficiently restore your state after the configuration change.
A scenario in which this can be valuable is if your application loads a lot of data from the web. If the user changes the orientation of the device and the Activity restarts, your application must re-fetch the data, which could be slow. What you can do instead is implement onRetainNonConfigurationInstance() to return an object carrying your data and then retrieve the data when your Activity starts again with getLastNonConfigurationInstance().
Look at here for more info Retaining an Object During a Configuration Change you can also find example over their..
Thanks..
Go to Manifest and add:
android:configChanges="orientation|screenSize"
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"
android:configChanges="orientation|screenSize"
/>
In Main Activity: Copy and Paste this code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
All Done -