我遇到了两个 ActionBarActivities 的问题。我有我的第一个活动(WorkoutEditActivity),它从 WorkoutEditActivity 内的片段中使用 startActivityForResult 启动另一个活动(A2)。当我从 A2 按下 ActionBar 上的后退按钮时,我的第一个 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 永远不会被调用,并且 onCreates savedInstanceState 也不包含信息。如何保存或恢复锻炼(以及 whatShouldBeSaved,实际上只是一个调试变量)?
PS:我把我的代码放在片段js标签中,因为它很长