the aim is to change the image resource of the button. If I don't change the orientation it works correctly, but if I change the orientation, the image resource is not set (or probably not updated). I observe in DebugMode that the following code (toggleButton method) is always executed, regardless the screen orientation is changed. The buttonIconID is also always correct. The problem is, that the image resource of the button is not set after changing the orientation.
@Override
public void onCreate(Bundle savedInstanceState) {
...
mTopBar = new TopBar(this);
...
}
TopBar constructor:
public TopBar(MainActivity mainActivity) {
this.mainActivity = mainActivity;
mButton = (ImageButton) mainActivity
.findViewById(R.id.toggleButton);
mButton.setOnTouchListener(this);
...
}
toggleButton method in TopBar:
public void toggleButton(final int buttonIconID) {
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mButton.setImageResource(buttonIconID);
}
});
}
Thanks for your help!
An Activity
is recreated every time you change the orientation. So when you change the orientation all views are reseted to their standard configuration (or the settings you have set in the onCreate()
/onResume()
etc methods.
So if you want to keep the image resource you have set programmatically after changing the orientation, you should buffer the data and set it again in the onCreate()
or onResume()
method.