Skip to content

Instantly share code, notes, and snippets.

@rosuH
Last active November 3, 2017 08:11
Show Gist options
  • Save rosuH/f90f8a7253d6742d6ff98f52d247f601 to your computer and use it in GitHub Desktop.
Save rosuH/f90f8a7253d6742d6ff98f52d247f601 to your computer and use it in GitHub Desktop.
Save data to deal with auto-rotate
/**
* 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