tags:

views:

119

answers:

2

Hi

Does android reuse views during a orientation change? I have a progress bar in my activity. I set the progress to 50%, and then do an orientation change. But for some reason, the progress bar maintain 50% after the orientation. I have call setprogress(0) in the onCreate() of my activity. And I have put 'printf' in anywhere I call setProgress(), i don't still don't understand why the progress bar maintains 50% everytime I do an orientation change?

Thank you for any help.

A: 

looks like you are supressing the onCreate method on a configuration change for the configuration including orientation/keyboardHidden in your manifest file. remove the android:configChanges from your activity in the manifest XML, and remove the public void onConfigurationChanged(Configuration newConfig) method from your activity.

Or, just set the progress of the progressbar to 0 in your onConfigurationChanged method

if you are implementing the onConfigurationChanged method, make sure you have it defined in the manifest XML file for the desired activity.

for example:

<activity
      android:name="MyActivity"
      android:configChanges="orientation|keyboardHidden" />

onConfigurationChanged will then be called when orientation or keyboardHidden configurations occur

binnyb
i have over-loaded onConfigurationChanged method in my activity, but it never gets called. I don't have any android:configChanges in my manifest xml file.
michael
OKay, but I want the onCreate() to be called whenever an orientation change occurred. I don't have android:configChanges in my Manifest file. and I don't have onConfigurationChanged method. And I do have setProgress(0) in my onCreate(), why the progress bar is **not** empty when there is an orientation change?
michael
add the relevant code and manifest to your question so maybe we can take a visual look at whats going on!
binnyb
A: 

Your Activity gets torn down and recreated, but Views with IDs set save their instance state to pass through to their future selves after they are recreated. This is restored in onRestoreInstanceState. Since onRestoreInstanceState is called after onCreate, the value you set in onCreate is overwritten by the previously saved data.

If you want to manipulate your views after this step happens, onPostCreate may be what you're looking for.

If you'd like to know more about how this process works (potentially to implement this save/restore behavior in your own custom views), see View#onSaveInstanceState and View#onRestoreInstanceState

adamp