Reputation: 3814
In an Android app, when you rotate the screen, onCreate()
is called to paint the view and create the objects. The TextView objects recover the previous texts but the ImageView objects not. Is there a reason for that? any answer will help me to understand how Android works :)
Thank you so much,
Upvotes: 0
Views: 47
Reputation: 49817
On orientation change the whole Activity is recreated. If you want to recover data after orientation change you can do that with onSaveInstanceState and onRestoreInstanceState. When you change the orientation onSaveInstanceState is called to save values in a Bundle and after the Activity is recreated onRestoreInstaceState is called to load the values from the Bundle again. You can use this like this:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// "Value" is a tag with which you can load the String again in onRestoreInstanceState.
savedInstanceState.putString("Value", this.stringToSave);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Use the tag "Value" to load the String again.
this.stringToSave = savedInstanceState.getString("Value");
}
Upvotes: 1