我在两个ActionBarActivities中遇到了问题。 我有第一个Activity(WorkoutEditActivity),它从WorkoutEditActivity中的Fragment使用startActivityForResult启动另一个Activity(A2)。 当我按下A2的ActionBar上的Back按钮时,我的第一个Activity被销毁,并在保存数据后通过onSaveInstanceState重新启动Activity,但是我无法弄清楚如何恢复数据。 这是相关的代码,但不起作用:
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;
}
}
下面是我的日志摘录
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从未被调用,而onCreate的savedInstanceState也不包含信息。如何保存或恢复锻炼(以及实际上只是调试变量whatShouldBeSaved)?
PS:我将我的代码放在snippet js标签中,因为它变得相当长。