有人可以解释为什么我的savedInstanceState中的值为null? 我有3个小部件,一个EditText,Button和TextView。这个人输入他们想要的东西。短语在TextView中弹出。我想在翻转手机时保持输入。我尝试保存状态但是当重新创建Activity时,我的Toast说它是null:
public class MainActivity extends AppCompatActivity {
private EditText input;
private TextView output;
private Button button;
private String newString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText)findViewById(R.id.input);
output = (TextView) findViewById(R.id.output);
button = (Button)findViewById(R.id.button);
if (savedInstanceState!=null){
Toast.makeText(this, "SAVED IS " + savedInstanceState.getString("example"), Toast.LENGTH_SHORT).show();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newString = input.getText().toString();
output.setText(newString);
}
});
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString("example",newString);
}}
答案 0 :(得分:3)
您需要使用此功能lapply(mylist, ReportOp)
,即没有names
data.frame
void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState)只有在清单
中的活动代码中指定了属性list
时才会调用此方法
您可以阅读更多about it here
答案 1 :(得分:1)
您可以定义一个对您的活动来说是全局的String
变量,并在restoringInstanceState上定义它。
看起来像这样的东西:
String userInput;
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
userInput = savedInstanceState.getString("example") // Refers to your "outState.putString "example" <-- key
output.setText(newString);
答案 2 :(得分:1)
使用以下代码对我有用。
electron .