I am having problems with two ActionBarActivities. I have my first Activity (WorkoutEditActivity) that starts another Activity (A2) with startActivityForResult from a Fragment inside the WorkoutEditActivity. When I press the Back Button on the ActionBar from A2 my first Activity gets Destroyed, and saves the Data in onSaveInstanceState and then the Activity gets restarted, but I cannot figure out how to restore the Data. Here is the relevant Code which does not work:
public class WorkoutEditActivity extends ActionBarActivity{
int whatShouldBeSaved = -1;
Workout workout = null;
ViewPager viewpager;
ViewFactory factory = new ViewFactory(getSupportFragmentManager());;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("onCreate", "called");
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_edit_activity);
viewpager = (ViewPager) findViewById(R.id.viewPager);
viewpager.setOffscreenPageLimit(0);
viewpager.setAdapter(factory);
viewpager.setOnPageChangeListener(
new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
SettingsFragment.isActivated = true;
} else {
if (SettingsFragment.isActivated) {
SettingsFragment.isActivated = false;
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
);
if (savedInstanceState != null) {
whatShouldBeSaved = savedInstanceState.getInt("test", -1);
if (savedInstanceState.getSerializable("workout") != null){
setWorkout((Workout) savedInstanceState.getSerializable("workout"));
}
Log.d("GOT SAVED DATA", "" + whatShouldBeSaved);
}
Log.d("CREATE", "created? " + workout);
Log.d("CREATE", "created? " + whatShouldBeSaved);
}
public void setWorkout(Workout w){
Log.d("WORKOUT", "had workout " + workout);
workout = w;
factory.setWorkout(workout);
Log.d("WORKOUT", "has workout " + workout);
}
@Override
public void onResume() {
super.onResume();
Log.d("RESUME", "resuming act");
SettingsFragment.isActivated = true;
//factory.setViewPager(viewpager);
if (getIntent().getExtras() != null){
if (getIntent().getExtras().containsKey("workout")){
Log.d("RESUME", "resume has extra");
setWorkout((Workout) getIntent().getSerializableExtra("workout"));
whatShouldBeSaved = getIntent().getIntExtra("test", -1);
}
} else {
Log.d("RESUME", "extras did not contain workout2");
}
Log.d("RESUME", "created? " + workout);
Log.d("RESUME", "created? " + whatShouldBeSaved);
}
@Override
public void onPause(){
super.onPause();
Log.d("ONPAUSE", "called");
}
@Override
public void onStart(){
super.onStart();
Log.d("START", "starting act");
}
@Override
public void onStop(){
Log.d("STOP", "stopping act");
super.onStop();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.d("onACTRES", "called");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d("SAVEINSTANCE", "called");
savedInstanceState.putInt("test", whatShouldBeSaved);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
Log.d("RESTOREINSTANCE", "called");
whatShouldBeSaved = savedInstanceState.getInt("test", 1);
Log.d("GOT SAVE", ""+ whatShouldBeSaved);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState){
super.onRestoreInstanceState(savedInstanceState, persistentState);
Log.d("RESTOREINSTANCE2", "called");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d("DESTROY", "onDestroy called");
Log.d("DESTROY", "destroying " + workout);
Log.d("DESTROY", "destroying " + whatShouldBeSaved);
Log.d("DESTROY", "was destroyed");
//first = true;
}
}
And here is an excerpt from my Log
8151-8151/localhost.fitnessoptimizer D/STOP﹕ stopping act
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ onDestroy called
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying Luffy
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ destroying -1
01-10 13:26:45.110 8151-8151/localhost.fitnessoptimizer D/DESTROY﹕ was destroyed
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer D/onCreate﹕ called
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer V/Monotype﹕ SetAppTypeFace- try to flip, app = localhost.fitnessoptimizer
01-10 13:26:45.118 8151-8151/localhost.fitnessoptimizer V/Monotype﹕ Typeface getFontPathFlipFont - systemFont = default#default
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer W/ViewPager﹕ Requested offscreen page limit 0 too small; defaulting to 1
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? null
01-10 13:26:45.126 8151-8151/localhost.fitnessoptimizer D/CREATE﹕ created? 1
01-10 13:26:45.127 8151-8151/localhost.fitnessoptimizer D/START﹕ starting act
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ resuming act
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ extras did not contain workout2
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? null
01-10 13:26:45.129 8151-8151/localhost.fitnessoptimizer D/RESUME﹕ created? 1
01-10 13:26:45.152 8151-8151/localhost.fitnessoptimizer D/GOT WORKOUT﹕ null
onRestoreInstanceState never gets called and onCreates savedInstanceState also does not contain the information. How can I save or restore the workout (and whatShouldBeSaved, which is actually just a debugging variable)?
PS: I put my code in snippet js tags, because it got quite long
From official site onSaveInstanceState
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).
When you press BACK at activity, it will be closed, but not killed. "Killing" could be done by Android system, when app is at background and system need to free some resource temporary.
You need to use Shared Preferences instead.