I am creating a counter. I have a stats page which the user can go to and check out stats. When you leave the counter activity by clicking back, the activity is destroyed and the data is lost. i want the users to be able to go back and forth between activities using the back button without loosing all the user data. Any suggestions?
Asked
Active
Viewed 1,028 times
0
-
1How about this link http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Ajay Takur Nov 12 '13 at 05:43
1 Answers
0
1.OverrideonSaveInstanceState
to save data,like this:
@Override
protected void onSaveInstanceState(Bundle outState) {
// save data
outState.putInt("key", 7);//your data
super.onSaveInstanceState(outState);
}
2.Recover data in onCreate
,like this:
// check if there is a saved data
if (savedInstanceState != null) {
int data = savedInstanceState.getInt("key"); //get your data,here get 7
} else {
//no save data,init it
}

LL520
- 159
- 3