简体   繁体   中英

Recreating activities in Android

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,

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"); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM