您需要重写 onSaveInstanceState(Bundle savedInstanceState) 并将您要更改的应用程序状态值写入 Bundle 参数,如下所示:
你可以像这样保存每个变量的实例
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("myBoolean", false);
savedInstanceState.putDouble("myDouble", 1.121);
savedInstanceState.putInt("myInt", 0);
savedInstanceState.putString("myString", "Your String");
// etc.
}
你可以像这样检索这些变化
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("myBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("myInt");
String myString = savedInstanceState.getString("myString");
}
希望这对您有所帮助。干杯!
更新:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
p.set("orientation", "portrait");
p.set("rotation",90);
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
p.set("orientation", "landscape");
p.set("rotation", 90);
}
并像这样设置属性:
Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);