I have two layouts for one activity like layout-port and layout-land, with different designs. If the user clicks the button in portrait mode and changes to landscape mode, the activity restarts or refreshes again. Can anybody tell me how to avoid this? Can anybody tell me how to maintain the state activity? Can anybody provide an example?
Thanks in advance
When the Activity goes from portrait to landscape the onCreate method is executed again, thus creating the button again. Thus, you should store the state of the button.
The best way is to use the onSaveInstanceState(Bundle) in order to store the state of the button. Then, when the Activity is created again, you should check the bundle and configure your layout accordingly. For more information, please check here: http://developer.android.com/reference/android/app/Activity.html (Section: Configuration changes).
Hope this helps!!
Related
I am sorry, this question has been asked probably thousand times over, but I do not find the correct words how to ask this question.
I have an activity in android. When I turn the screen at 90 degrees, it looks like the current activity is completely destroyed and a new activity is initiated. Is that correct?
But if I have showed some data in one orientation, I want to show the exact same data in the other orientation? Is there some SIMPLE way to achieve this? That the activity just remains, but shows the layout with the SAME data from the SAME activity in the other orientation?
there is an easy way to save your time, if your project api level is upper than Android 3.2 (API level 13) add this code into your activity in manifest:
android:configChanges="keyboardHidden|orientation"
Update: this also can help:
android:configChanges="keyboardHidden|orientation|screenSize"
Yes whenever an activity changes its rotation it is destroyed and recreated. and the same android uses same layout and roughly shows it into landscape mode.
You can make a new folder layout folders name layout-land.
layout-land may contain the same layout as in layout folder and the views can be tweeked according to the landscape orientation requirments
Another way would be to store all your information in savedInstanceState and restore it from there in onCreate(). Or use a Fragment with setRetainInstance(true) to store your information in.
I would like to know why when screen orientation is changed current tab changes back to default one in TabHost?
I understand that the Activity is destroyed and created again, but why the state of TabHost isn't saved? Per example, text of an EditText is saved and restored, why is it different for current tab? Do I have to do it myself?
Thanks
You are responsible for managing your own tabs ( fragment transactions). So it follows that if you want a particular tab to be selected then you have to save state information prior to the configuration change.
This may help (especially if you follow the link in the answer):
How can I prevent the current tab view from being lost when rotating the screen?
By default, EditText saves its own instance. See Yalla T's answer here:
How to retain EditText data on orientation change?
Hi Everyone iam new to android and stuck with the orientation problem i need to display separate layout in landscape and portrait which i designed separately and placed in layout-large and layout-large-land folders now i need to change layout when device is rotated to landscape with out destroying and recreating the Activity
please help me get out of this problem
Thanks in Advance
my advice as a long time Android programmer is:
Don't do it!
Let the activity be destroyed and re-built with the correct layout.
Just search and research on all the several methods of keeping the data during orientation changes and apply them to your specific case. Below a few to illustrate:
the onCreate(Bundle) receives that bundle that contains information saved during onSavedInstances(Bundle);
User a fragment without a UI (do not call onCreateView) and set it to be retained across rotation with setRetainInstance(true) and use it to remember the data
use the Loader pattern to automatically receive the data it was generated on the previous activity
Replace
layout-large-land
with
layout-land-large
Prevent activity from recreating/destroying
Add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.
The purpose of the android:configChanges attribute is to prevent an activity from being recreated when it's really required.
Let me know if it works for you..
Try this,
Add this code in your mainfest.xml each and every activity.
android:ConfigChanges="keyboardHidden|orientation"
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);
}
My app lets users change the text and visibility on some TextViews. I want to offer a button that will undo all changes and return the layout to it's original settings.
If you turn the phone from portrait to landscape, it does just that... but how do I trigger it from a button?
You could invalidate the base View or call Activity.setContentView
If you turn the phone from portrait to landscape, it does just that... but how do I trigger it from a button?
The recreate method of the Activity class seems to do just that. Quote from the docs:
Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.
An example "configuration change" mentioned above is orientation change, i.e. switching from landscape to portrait or the other way around.