我正在做一个Android应用程序,其中我有两个不同的布局用于相同的活动(一个用于纵向,一个用于横向,重要的是说它们完全不同)。好吧,我的问题是它们之间的数据持久性,因为当在纵向和横向之间切换时数据丢失,试图解决我的问题我使用onSaveInstanceState并更改清单,但在我的情况下它们都不起作用。我希望你能帮助我,问候。
概率pd。 Landscape位于目录layout-land
中mensaje=(EditText) findViewById(R.id.EditTextAlarma); //portrait
horaEdit=(EditText) findViewById(R.id.editTextHoras); //landscape
minutosEdit=(EditText) findViewById(R.id.editTextMinutos); //landscape
segundosEdit=(EditText) findViewById(R.id.editTextSegundos); //landscape
mensajeTimer=(EditText) findViewById(R.id.mensajeTimer); //landscape
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("msj",mensaje.getText().toString());
outState.putString("hora",horaEdit.getText().toString());
outState.putString("min",minutosEdit.getText().toString());
outState.putString("seg",segundosEdit.getText().toString());
outState.putString("msjT",mensajeTimer.getText().toString());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String m=savedInstanceState.getString("msj");
String h=savedInstanceState.getString("hora");
String mn=savedInstanceState.getString("min");
String s=savedInstanceState.getString("seg");
String mt=savedInstanceState.getString("msjT");
mensaje.setText(m);
horaEdit.setText(h);
minutosEdit.setText(mn);
segundosEdit.setText(s);
mensajeTimer.setText(mt);
}
答案 0 :(得分:2)
然后当方向更改时,只需获取 onRestoreInstanceState 中的值并将其设置为xml小部件
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//SAVE YOUR DATA HERE
outState.putString("key","Value");
//YOU CAN SAVE ANY TYPE OF DATA HERE AND RETRIVE
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//RETRIVE YOUR DATA HERE
String value=savedInstanceState.getString("key");
//SET VALUE TO XML HERE
Log.d("Value saved is:",value);
}