I would like to know what the above mentioned attribute for an Activity in the AndroidManifest.xml does and why it (would be) needed?
I have already read the Android documentation about this topic and the explaination is not quite clear to me. Id like to know an example case WHY I would have to set this attribute.
As you may have known every time a parameter of the phone changes the system rebuilds the whole Activity in order to load the new resources. On of these parameters is the screen size, which can change on a phone, as the rotation of the phone changes it.
If you define android:configChanges in your manifest you can have full control over your application, which means that the system won't destroy your Activity, it only calls the onConfiguratinChanged method of it. This way you have to manage the resizeing of the screen.
Related
Whenever I use setRequestedOrientation(int) it behaves differently from when I declare the orientation in my Manifest.xml.
(this is expected)
I read that (from docs):
If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted)
Practically this means that whenever an app is started, while the device has an orientation that is NOT the orientation that will be set with setRequestedOrientation(int), the activity will be created twice (once 'normally' and once to reflect requested orientation).
As of now it is not worthwhile to make my activites 100% config-change-proof, what I would like is a way to set the requested orientation, in code, without having to create my activities twice. Is there a way?
(Preferably without using hackish workaround such as setting the 'orientation' flag in configChanges)
EDIT:
Accepted answer is right, it simply isn't logical (doesn't make sense). For new readers, what I did was add a stub-activity (mainly empty activity that doesn't do much). All it does is use getRequestedOrientation() when it differs from what I want it to be, I will call setRequestedOrientation(int), otherwise start the activity I want and in its onCreate also call the setRequestedOrientation(int) to keep it in requested orientation.
No, that doesn't make sense. If you set the orientation programmatically, the Activity is already running before you can do anything to change the duplicate creation for each orientation.
My requirement is as follows:
I have two modes of my application in first mode it will open normally but in second mode I have to re size all the screens to a fixed dimension. I am able to do so by writing code in each activity before calling setContentView() method, what I want to know is, Does there any optimal way of doing the same i.e writing the code at single place or anything else so that I can optimize the code.
I am looking for it because in second mode I will set some parameters, And my application then required to check the parameter in activities individually which in terms effect my performance, that's why I want something which runs at starting of my app and re size all the activities view.
any help would be greatly appreciated!!
Edit:
Can we specify the dimensions in the attrs.xml and dynamically set the specified theme?
Have you tried using Fragments? http://developer.android.com/guide/components/fragments.html
I have alternative layouts in my layout-normal-land and layout-normal-port folders and they are correctly invoked by the system according to if I hold the device in land or port at start. My problem is, when I rotate the device AFTER I have launched the app, it tries to somehow adapt the already displayed view to the new situation, creating a mess.
How can I tell the system it should switch to the alternate layout during execution?
Have been experimenting around and found that when I dont have android:configChanges="keyboardHidden|orientation" , oncreate gets called again, which gives the correct layout, but its not what I want to have. I dont think this is normal, is it? Maybe a question of bug in Android (2.3.3.)?
By including
android:configChanges="orientation"
in your manifest you are saying you want to handle orientation changes yourself. You should remove it if you want the system to handle it for you.
The automatic handling works extremely well. You should only override it if you have a specific reason for doing so.
In normal operation (without the above manifest entry), an orientation switch causes the current activity to be closed and then re-opened in its new orientation reloading all resources and layouts from the currently active resource folders. The process follows what is known as the "Activity Lifecycle".
If you include the above manifest entry, you are saying, "I will handle all changes myself. Do not close my activity" so it is then your responsibility to remove all unwanted layouts from the activity and replace them with the layouts you now require for the current orientation.
Do you already use an OrientationListener?
If no:
http://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)
the listener should detect orientation changes.
Then you call setContentView (R.layout.name_of_layout) in your Activity class.
Hope this helps.
do you have android:configChanges="orientation" in your manifest on that activity? That will prevent android from automatically changing your layout.
May be obvious, but:
Within the res folder make sure you have the folders labelled "layout" and "layout-land".
Portrait and landscape .xml files must have the same filename.
I have run into the same problem, and I did not have android:configChanges="orientation" in my manifest.
However I did have [Activity(ConfigurationChanges = ConfigChanges.Orientation)] in my mainactivity. That seemed to be another way to override the automatic orientation handling.
I was wondering why not use android:configChanges="keyboardHidden|orientation" in every (almost every ;)) activity?
Goods:
no need to worry about your activity been rotated
it's faster
Not so nice:
need to change your layouts if they are depending on screen size (e.g. layouts with two columns or so)
Bad:
no flexible way to have different layouts on different orientation
not so good when using fragments
But if we don't use different layouts, why not?
Quick Background
By default, when certain key configuration changes happen on Android (a common example is an orientation change), Android fully restarts the running Activity to help it adjust to such changes.
When you define android:configChanges="keyboardHidden|orientation" in your AndroidManifest, you are telling Android: "Please don't do the default reset when the keyboard is pulled out, or the phone is rotated; I want to handle this myself. Yes, I know what I'm doing"
Is this a good thing? We shall soon see...
No worries?
One of the pros you start with is that there is:
no need to worry about your activity been rotated
In many cases, people mistakenly believe that when they have an error that is being generated by an orientation change ("rotation"), they can simply fix it by putting in android:configChanges="keyboardHidden|orientation".
However, android:configChanges="keyboardHidden|orientation" is nothing more than a bandaid. In truth, there are many ways a configuration change can be triggered. For example, if the user selects a new language (i.e. the locale has changed), your activity will be restarted in the same way it does by an orientation change. If you want you can view a list of all the different types of config changes.
Edit: More importantly, though, as hackbod points out in the comments, your activity will also be restarted when your app is in the background and Android decides to free up some memory by killing it. When the user comes back to your app, Android will attempt to restart the activity in the same way it does if there was some other configuration change. If you can't handle that - the user will not be happy...
In other words, using android:configChanges="keyboardHidden|orientation" is not a solution for your "worries." The right way is to code your activities so that they are happy with any restart Android throws at them. This is a good practice that will help you down the road, so get used to it.
So when should I use it?
As you mentioned there is a distinct advantage. Overwriting the default configuration change for a rotation by handling it yourself will speed things up. However, this speed does come with a price of convenience.
To put it simply, if you use the same layout for both portrait and landscape you're in good shape by doing the overwrite. Instead of a full-blown reload of the activity, the views will simply shift around to fill the remaining space.
However, if for some reason you use a different layout when the device is in landscape, the fact that Android reloads your Activity is good because it will then load up the correct layout. [If you use the override on such an Activity, and want to do some magical re-layout at runtime... well, good luck - it's far from simple]
Quick Summary
By all means, if android:configChanges="keyboardHidden|orientation" is right for you, then use it. But PLEASE be sure to test what happens when something changes, because an orientation change is not the only way a full Activity restart can be triggered.
From my point of view: If the layout is the same in both landscape and portrait mode - you might aswell disable one of the two in your app.
The reason why I state this is that I as a user expect the app to provide me with some benefit, when I change orientation. If it doesn't matter how I hold my phone, then I don't need the choice.
Take for instance an app where you have a ListView, and upon clicking a ListItem you want to be shown a detailed view for that item. In landscape you would od this by dividing the screen in two, having the ListView on the left and the detailed view on the right. In Portrait you would have the list in one screen and then change the screen to the detailed view when a ListItem is selected. In that case orientation change makes sense as well as different layouts.
I don see why.... occasional restarts are ok in my opinion... configChanges handles most cases for me... well maybe in some types of applications this can be problem but it depends really on type of app and how you restore state when app restarts... When one of my app restarts user is logged back and last activity opens by my code and user jus loses some steps to go back where he was but not big deal.. In other some state is always persisted and some state is always restored on restart. When activity restarted it had to be that app have not been used or something... so no problem at all... In game for example this can be problem maybe or in some other type of app I don't know...
I say that when you do it this way applications just works fine under normal circumstances. And code is much more readable without ton of logic needed for saving and restoring where u just can make new bugs and have to maintain it all the time... sure if android gets out of power and kill you application window it lose the context and starts again, but this happen just in special situations and on newer devices I belive this is more and more rare...
So kill me, but I use this across applications quite successfully...
android:configChanges="locale|keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
But I understand that for some special kind of applications it may be not good way but most of apps can live with this just OK.
Yeah I think pausing will make it quicker than releasing the player. Still have the pause though.
Have now found a solution that won't pause the song.
State in the manifest that you will handle the config change for screen orientation and then use the onConfigurationChanged method to load the layout file. By doing this in logCat I can see onPause, onCreate & onResume aren't called, and therefore the song isn't paused.
update the manifest to handle the orientation.
android:configChanges="orientation|screenSize"
add this code
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
Documentation says: "a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy()."
I would like to suspend the orientation change, since it crashes my app if it was done in the middle of a a loop (of reading a file).
How can I do this? Also - looking for some kind of "onOrientationChnage" function :)
First, why not solve the initial problem instead?
90% of the times, the problem that you are facing (when changing orientation) is solved in this question:
Background task, progress dialog, orientation change - is there any 100% working solution?
Read it, correct your code and see if the problems are gone!
If you still want to do it anyway:
Quoting the documentation:
Note: Handling the configuration
change yourself can make it much more
difficult to use alternative
resources, because the system does not
automatically apply them for you. This
technique should be considered a last
resort and is not recommended for most
applications.
Still want to do it? Read the following link (where I also found the quotation):
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
If you rely on android system to use different layouts when you are on portrait/landscape mode, you'll need to start to handle these differences by yourself. If you don't use these features, you may get away without doing nothing fancy :)
If you always want a particular orientation, you can add android:screenOrientation="portrait" (or ="landscape" as an attribute to your tag in the manifest.
At run time, you can call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);