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?
Related
Application has two fragments: the first one contains a small representation of pager with photos, and the second one contains full screen pager. The second fragment replaces the first and passes a page number to the previous every time it changes. I made connection between my fragments just like Android Developers says.
Everything works till device orientation doesn't change. The first fragment is not recreated until it is not on top of stack, that is why all page number changes after that are missed for first fragment.
I do not really what to disable views destroy on orientation change, but looks like it is the only way.
What is the best solution?
In your manifest file in your parent activity in which fragments are write following line :
android:configChanges="keyboardHidden|screenSize|orientation"
Let me know if that works for you. Best of luck :)
When you change the orientation, the activity gets rebuild, thus essentially destroying the fragment that was built before the change in orientation.
Perhaps you would wish to refer to this https://developer.android.com/guide/topics/resources/runtime-changes.html on retaining the state during a change in configuration.
I use framgents in my app and I have a dual pane layout for both landscape (left and right) and portrait (up and down) on a tablet.
I have an activity which loads a fragment into the left selection pane and then you can choose several criteria in some Spinner to do a search. In right fragment is being shown results from search in a ListView.
If I use android:configChanges="orientation" in this activity in Manifest.xml the results from search are saved but I have both pane layouts up and down in landscape mode instead of left and right, as it should be.
And if I do not use android:configChanges="orientation" I have both pane layouts in order correct (left and right in lanscape mode) but I do not have results from search, activity restarts.
How I can solve it? Any idea?
Thanks so much.
You should be using onSaveInstanceState() and onRestoreInstanceState()
You need your view to be recreated for the layout to look correct but on recreation the data will be lost. You must save this data using the methods I mentioned or by using the shared preferences (for saving data longer that lasts after application exit).
Do not use android:configChanges="orientation". This prevents the layout from being recreated and expects you to deal with it yourself.
See the documentation for more info:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
http://developer.android.com/reference/android/content/SharedPreferences.html
I know saveInstanceState() is used to store activity variables, text in EditText, etc.
But I have a doubt that should I save state of view?
Let me give you a scenario. My view has 3 buttons. On clicking one of them, a WebView is displayed to user (in same activity). Now if app gets killed, should I save state that user was displayed WebView when app got killed and when activity gets recreated display WebView instead of buttons?
Other scenario is, I have 3 tabs in view. Selecting each tab shows different view. As explained in above case again should I save that user has last selected this tab?
It will be best if you can explain the cases where I should and should not save activity state.
The operating system knows when it should re-create your app's previous state (the screen orientation changed or your app was killed in the background by the OS) and when to create a new instance (the user left your app with the back button). The onRestoreInstanceState() method is only called when there's a state to restore (when the system is restoring a previous state, as opposed to creating a new instance of the activity).
The short answer, then, is that if you override onSaveInstanceState() and onRestoreInstanceState(), the system will call them when appropriate, and you don't have to worry about deciding when you "should" save state.
When overriding onSaveInstanceState(), yes, you should save everything about your activity's state. This is the method being used during screen orientation change. Think about it - if you rotate your phone, do you expect the current app to change tabs, or the screen that just opened to disappear?
For more information, see the Android documentation on recreating an activity.
I have not done very much research on savedIntanceState on app gets killed. But yes you may save maybe a integer variable (referring to which button is clicked) in state, so that when activity is recreated, you know which webview used to be shown (or none). Same goes to your second situation.
Some extra use case of saved instance state:
One of the most used scenario is during user switches orientation, say you have a couple of edit texts on screen, their holding texts would be gone if user change his device orientation. Saved instance state helps you to recover the entered texts.
Another situation is you will most likely have a few class variable in your activity, probably used to save what user has done, or some temporary list object in a list activity. Saving those variables also prevents you from needing to recover the data on orientation change.
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!!
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.