Screen orientation change forces data re-entry
I'm developing a simple app, only a handful of activities and nothing to complicated. The issue I'm having is that in a few of my activities I have EditText开发者_运维知识库 widgets, and when the screen is rotated, these widgets loose their values. They still visually, that is, on the handset screen, report their values from before the rotation, but to the code report their default values.
An example, I have an activity with an EditText, I enter a number, 15, the calculator starts calculating, displays a result. If at this point I rotate to a different orientation, as expected the activity dies, reloads, shows the new orientation, my value of 15 is still in the EditText, however the calculator (the code) calculates as if it was 0, which is the initial value. I have not tried changing the initial value to see if it is really pulling from the default value, or just setting itself to 0 when I cast it as an int.
If I didn't explain this well enough, let me know, I can add source code if I need to. Thanks.
When are you reading the values? View state is preserved by Activity
's default implementation of onRestoreInstanceState
, which happens between onStart
and onPostCreate
. If you are reading the values before this happens, that would explain why you are seeing default values for your calculation rather than the values that are displayed once the screen is drawn.
精彩评论