0

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?

BrokenCodez
  • 53
  • 1
  • 8

1 Answers1

0

1.OverrideonSaveInstanceStateto 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