I'm attempting to save some data in intent, so when the user visits another screen or presses the back button, and comes back, the data persists. Essentially storing the state of a screen.
So you're in Fragment 1, you navigate to Activity 2 (important data is here), if you leave activity 2 by pressing back or through another way when you come back the data is still there.
Here's where I start Activity 2 from Fragment 1
intent = new Intent(getActivity(), DeliveryPageActivity.class);
...
startActivityForResult(intent,
Here's where I'm currently trying the solve the problem
@Override
public void onBackPressed() {
//saving here (working okay)
storeIntent.putExtra("currentDelivery", pageIndicatorView.getCurrentPage());
setResult(RESULT_OK, storeIntent);
super.onBackPressed();
}
@Override
protected void onResume() {
super.onResume();
//restoring here (NOT WORKING, intent has no extras)
if (storeIntent.hasExtra("currentDelivery")) {
currentDelivery = storeIntent.getIntExtra("currentDelivery", currentDelivery);
pageIndicatorView.setCurrentPage(currentDelivery, true);
}
}
And starting a new intent in onCreate()
storeIntent = new Intent();
Try override onPause instead of onBackPressed in your Activity 2. See docs for activity lifecycle
Edit: Sorry just tested. In your case, i would use SharedPreferences to restore and override OnPause (get's called when back button is pressed) and onResume like this:
@Override
protected void onPause() {
super.onPause();
SharedPreferences sp = this.getSharedPreferences("myPrefsName", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("currentDelivery",pageIndicatorView.getCurrentPage());
editor.apply();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sp = this.getSharedPreferences("myPrefsName", MODE_PRIVATE);
testState = sp.getInt("currentDelivery", 0);
}