我正在尝试使用onsaveinstancestate和onrestoreinstancestate旋转设备时保存值但是当我尝试在方向上获取oncreate中的数据时它总是返回null.below是我的代码
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("driverscore",mdriverscore.getText().toString());
outState.putString("lastevent",recentevent.getText().toString());
outState.putString("rashdrivingcounttext",rdcount.getText().toString());
outState.putString("prespeedlimitcounttext",pslcount.getText().toString());
outState.putString("corneringcounttext",cornering.getText().toString());
outState.putInt("rashdrivingcount",mrdcount);
outState.putInt("prespeedlimitcount",mpslcount);
outState.putInt("corneringcount",mcornering);
outState.putInt("newrdcount",scorerd);
outState.putInt("newpslcount",scorepsl);
outState.putInt("newcornercount",scorecorner);
outState.putInt("distance",distance);
outState.putBoolean("istripinprogress",istripinprogress);
outState.putBoolean("ispersonidle",ispersonidle);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onRestoreInstanceState(savedInstanceState, persistentState);
mdriverscore.setText(savedInstanceState.getString("driverscore"));
recentevent.setText(savedInstanceState.getString("lastevent"));
rdcount.setText(savedInstanceState.getString("rashdrivingcounttext"));
pslcount.setText(savedInstanceState.getString("prespeedlimitcounttext"));
cornering.setText(savedInstanceState.getString("corneringcounttext"));
mrdcount = savedInstanceState.getInt("rashdrivingcount");
mpslcount = savedInstanceState.getInt("prespeedlimitcount");
mcornering = savedInstanceState.getInt("corneringcount");
scorerd = savedInstanceState.getInt("newrdcount");
scorepsl = savedInstanceState.getInt("newpslcount");
scorecorner = savedInstanceState.getInt("newcornercount");
distance = savedInstanceState.getInt("distance");
istripinprogress =savedInstanceState.getBoolean("istripinprogress");
ispersonidle = savedInstanceState.getBoolean("ispersonidle");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newxml);
if (savedInstanceState != null) {
istripinprogress = savedInstanceState.getBoolean("istripinprogress");
ispersonidle = savedInstanceState.getBoolean("ispersonidle");
mrdcount =savedInstanceState.getInt("rashdrivingcount");
}
mrdcount返回null istripinprogress,ispersonidle返回默认值而不是保存值
答案 0 :(得分:0)
您必须覆盖a="mom"
b='mom'[::-1] # reverse the string
if a==b: # if original string equals to reversed
print ("palindrome ")
else:
print ("not a palindrome ")
void onSaveInstanceState (Bundle outState)
这与onSaveInstanceState(Bundle)相同,但是需要它 使用persistableMode属性设置创建的活动 persistAcrossReboots。
答案 1 :(得分:0)
您使用的具体原因是什么
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
而不是
public void onSaveInstanceState(Bundle outState)
请使用单个参数功能,例如public void onSaveInstanceState(Bundle outState)
和public void onRestoreInstanceState(Bundle savedInstanceState)
,然后您的onCreate
将能够拥有捆绑包中的值。
希望这有帮助!
答案 2 :(得分:0)
onRestoreInstanceState是多余的,因为您可以在onCreate中轻松恢复状态。
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("driverscore",mdriverscore.getText().toString());
outState.putString("lastevent",recentevent.getText().toString());
outState.putString("rashdrivingcounttext",rdcount.getText().toString());
outState.putString("prespeedlimitcounttext",pslcount.getText().toString());
outState.putString("corneringcounttext",cornering.getText().toString());
outState.putInt("rashdrivingcount",mrdcount);
outState.putInt("prespeedlimitcount",mpslcount);
outState.putInt("corneringcount",mcornering);
outState.putInt("newrdcount",scorerd);
outState.putInt("newpslcount",scorepsl);
outState.putInt("newcornercount",scorecorner);
outState.putInt("distance",distance);
outState.putBoolean("istripinprogress",istripinprogress);
outState.putBoolean("ispersonidle",ispersonidle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newxml);
if (savedInstanceState != null) {
mdriverscore.setText(savedInstanceState.getString("driverscore"));
recentevent.setText(savedInstanceState.getString("lastevent"));
rdcount.setText(savedInstanceState.getString("rashdrivingcounttext"));
pslcount.setText(savedInstanceState.getString("prespeedlimitcounttext"));
cornering.setText(savedInstanceState.getString("corneringcounttext"));
mrdcount = savedInstanceState.getInt("rashdrivingcount");
mpslcount = savedInstanceState.getInt("prespeedlimitcount");
mcornering = savedInstanceState.getInt("corneringcount");
scorerd = savedInstanceState.getInt("newrdcount");
scorepsl = savedInstanceState.getInt("newpslcount");
scorecorner = savedInstanceState.getInt("newcornercount");
distance = savedInstanceState.getInt("distance");
istripinprogress = savedInstanceState.getBoolean("istripinprogress");
ispersonidle = savedInstanceState.getBoolean("ispersonidle");
mrdcount =savedInstanceState.getInt("rashdrivingcount");
}
}
这是官方文档对onRestoreInstanceState的说法:
大多数实现只是使用onCreate(Bundle)来恢复它们的状态,但有时在完成所有初始化之后在此处执行它或者允许子类决定是否使用默认实现。