i am new to android.i have one single activity
with main.xml
file. Now, i have one scroll view
in that main.xml
file.when i run my application in portrait mode
and when i go to the bottom of the scroll view
and than when i change my application state
to the landscape mode than i go to the top of the scroll view..that means when we switch between portrait
to landscape
or vice versa the activity
is recalled. so need to save the application state in portrait
and restore in landscape
. so any solution of it?
thanks in advance
Aamirkhan I.

- 13,560
- 9
- 60
- 109

- 5,746
- 10
- 47
- 74
-
Have you had a look at this http://stackoverflow.com/questions/2308628/how-do-you-save-your-activitys-state-when-exiting-android – rfsk2010 Dec 29 '11 at 12:35
1 Answers
The documentation does a decent job of explaining different ways to handle configuration changes, including screen orientation changes. One of those methods, which is good for saving temporary state of the UI, is saving data in the onSavedInstanceState()
method--as @Jason Kuang mentioned.
Generally, you can rely on Android to save and restore the state of View
s without any special effort on your part. The source code for the protected
method onSaveInstanceState()
explains (emphasis added):
The default implementation takes care of most of the UI per-instance state for you by calling android.view.View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(android.os.Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.
This is a little deceptive, because the API documentation states that EditText
s and TextView
s must have android:freezesText="true"
explicitly declared on them in your layout XML files to ensure that Android automatically stores their state when onSaveInstanceState()
is invoked. I have not tested this recently, but it is what the source code seems to be doing. Therefore, handling temporary UI state on your own is best.
Another tip: You can explicitly prevent the storage of temporary data for a View
by calling setSaveEnabled(false)
on that View
. (This will not affect its children.)
As a rule, it's a good idea to manually save the on-screen state in your onPause()
method, and also in onSaveInstanceState()
.

- 6,817
- 18
- 77
- 89

- 1,718
- 1
- 18
- 20
-
A colleague informed me that what the API doc says about `EditText`s and `TextView`s needing auto-saving explicitly enabled--as I mentioned above--isn't actually the case. I guess testing is always a good policy to make sure what the doc says is up to date. @AamirPathan, do you want to accept the answer you found most useful and accurate? – hotshot309 Jan 23 '12 at 16:41