2011-08-12 110 views
2

从以前的问题,请遵循onRestoreInstanceState不会被调用

一个第一屏幕上的活动,我点击一个按钮,这样

Intent intent = new Intent(MainMenu.this, NewClass.class); 
intent.putExtra("value1", value1); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

值传递给新的活动,并在onCreate() - 方法是使用

在新的活动,我节电状态,当我点击后退按钮

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    // the UI component values are saved here. 
    super.onSaveInstanceState(outState); 
    Toast.makeText(this, "Activity state saved", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onBackPressed() { 
    //super.onBackPressed(); 
    Intent intent = new Intent(RoundScoring.this, MainMenu.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
    startActivity(intent); 
    Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show(); 
} 

在第一个屏幕我再次点击不同的按钮具有以下:

Intent intentContiune = new Intent(MainMenu.this, NewClass.class); 
intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
startActivity(intentContiune); 

的Activty被加载但作为价值尚未设置总是失败。我写了一个onRestorInstanceState来重新从包中填充值,但它永远不会被解雇。有人会说我在做什么错

感谢您的时间

UPDATE

@Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     Log.d(DEBUG_TAG, "returnstate called"); 
     super.onRestoreInstanceState(savedInstanceState); 
     value = savedInstanceState.getString("value"); 


    } 

回答

1

使用应用程序

yourApplication.java

public yourApplication extends Application { 
    String Value; 
    public synchronized String getValue(){ 
     return Value; 
    } 
    public synchronized void setValue(String Value){ 
     this.Value = Value; 
    } 
} 

AndroidManifest.xml

<application android:name="yourApplication" ... 

现在你可以在任何地方使用保存值:

SAVE

((yourApplication) getApplicationContext()).setValue("something"); 

LOAD

((yourApplication) getApplicationContext()).getValue(); 
+0

SQLite数据库,但仍旧没有 –

+0

对不起,我编辑的问题: –

+0

但onResume不能与捆绑一起使用。正如你所看到的,我正在节省一笔价值 –

0

onRestoreInstanceState获取的活动时,活动后会重新初始化不叫开始.....等活动开始它会不能叫...

如果要保存一些数据,那么你可以使用sharedPreference像这种情况....

+0

麻烦的是,我会想一个ArrayList添加到捆绑,不相信的可以共享prefreences做到这一点 –

+0

那么你应该使用的尝试这种机器人 –

0

onRestoreInstanceState()是非常棘手的。我建议你使用SharedPreferences

http://developer.android.com/guide/topics/data/data-storage.html#pref

+0

麻烦是我想要添加一个ArrayList的捆绑,并不相信你可以做到这一点与共享prefreences –

+0

解决方法它。假设你有ArrayList的字符串,你可以通过迭代列表并将'int i = 0; for(String s:list){editor.putString(“pref”+ i,s);我++; }'然后调用'.commit();'这个逻辑对于加载首选项是一样的。 –

+0

对象ArrayList通过 –

0

检查

AndroidManifest。XML

应该包含

机器人:configChanges = “方向| keyboardHidden”

0

系统调用onRestoreInstanceState仅如果在你的活动恢复已保存的状态。正是由于缺乏资源而导致Activity被系统杀死时,它被用来保存保存在onSaveInstanceState中的状态。

如果你想保存一些信息并在任何时候都可用,那么就使用SharedPreferences。