I make a login form with dynamic field validation. I have 3 fields username, email & password & all of these field are required. When field length = 0, I set error
editText.setError( getText(R.string.cannot_be_blank) );
and this code works fine, but when I change the orientation, all the errors disappear
How to save error state?
Thanks.
When the orientation is changed the framework will recreate the Activity by calling
onCreate(Bundle savedInstanceState). Before the switch in orientation theonSaveInstanceState(Bundle outState)method will be called if it is overridden in your Activity.You can save the state of your errors in the Bundle passed into the
onSaveInstanceStatemethod. This bundle is passed to youronCreate()method as thesavedInstanceStateBundle.Therefore you need to override the
onSaveInstanceStatemethod in your Activity as follows (saving the state of your errors):Then in your
onCreatemethod check if thesavedInstateStateBundle is null or not. If not, you can retrieve the values out of it with the following code: