As usual in android, each time the screen is flipped to portrait/landscape mode an Activity runs through life-cycle from onSaveInstanceState to onDestroy and then is recreated.
In my Activity there’s a ButtonText which can be changed by the user. It’s reseted to the initial state, but I have to save the last state somehow. How can I achieve that, will I have to override onSaveInstanceState? Can someone show an example?
Step #1: Call
getText().toString()on theButtonto get the caption.Step #2: Call
putString()on theBundlepassed to your implementation ofonSaveInstanceState()to store the caption.Step #3: Call
getString()on theBundlepassed to your implementation ofonRestoreInstanceState()(or inonCreate(), if theBundleis notnull, if you wish) to get your caption back, then callsetText()on yourButtonto put the caption back in.Here is a sample project that uses
onSaveInstanceState()to save aUriof a contact (which also affects whether or not aButtonis enabled).