Last active
November 3, 2017 08:11
-
-
Save rosuH/f90f8a7253d6742d6ff98f52d247f601 to your computer and use it in GitHub Desktop.
Save data to deal with auto-rotate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* All activity data would be destoryed after the device rotate | |
* So we need save the data first and recall it after the device rotate | |
*/ | |
public class MainActiviy extends AppCompatActivity { | |
// example key variable | |
private static final String KEY_INDEX = "index"; | |
// example data variable | |
private static final int mData; | |
@Override | |
public void onCreate (Bundle saveInstanceState) { | |
super.onCreate(saveInstanceState); | |
// recall the variable | |
if (saveInstanceState != null) { | |
// get the data | |
mData = saveInstanceState.getInt(KEY_INDEX, 0); | |
} | |
// do something with data... | |
} | |
/** | |
* save the data before device rotate | |
*/ | |
@Override | |
public void onSaveInstanceState(Bundle saveInstanceState) { | |
super.onSaveInstanceState(saveInstanceState); | |
saveInstanceState.putInt(KEY_INDEX, mData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment