我有一个带对话框的活动。在单击ImageView时的对话框中,默认的相机应用程序启动,当我创建照片并单击刻度图标(至少它是我手机上的刻度图标)时,之前的活动将被重新创建,销毁并重新创建。这种情况大约发生在10次中。
这就是发生的事情
1. Intent opens camera
2. onPause()
3. onSaveInstanceState runs
4. onStop()
5. onDestroy()
6. Camera app opens, picture is taken and I click tick
7. onStart﹕()
8. onRestoreInstanceState runs
9. onResume()
10. onPause()
11. onSaveInstanceState
12. onStop()
13. onDestroy()
14. onStart﹕()
15. onRestoreInstanceState
因此,即使我在onSaveInstanceState中保存我需要的数据(步骤3)并且在onRestoreInstanceState中检索它(步骤8),它也会在活动被销毁时丢失(我使用一个标志来决定我是否想要保存数据,第二次重新创建活动时该标志变为null。我可以在SharedPreferences中保存所有内容,但是,这仍然是我想要纠正的不需要的功能。
如果重要,请参阅以下代码:
意图
btn_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
which_dialog = "complete";
complete_dialog.dismiss();
imageFileForCamera_ = getTemporaryDirectory();
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (imageFileForCamera_ != null) {
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFileForCamera_));
}
startActivityForResult(intentPicture,PICK_FROM_CAMERA_COMPLETE_KITKAT);
onActivityResult:
case PICK_FROM_CAMERA_COMPLETE_KITKAT:
selectedImageUri = Uri.fromFile(imageFileForCamera_);
complete_dialog.show();
(...more code...)
的onSaveInstanceState
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (which_dialog != null && which_dialog.length() > 1) {
outState.putString("which_dialog", which_dialog);
outState.putSerializable(INSTANCE_STATE_IMAGE_FILE_FOR_CAMERA, imageFileForCamera_);
} else {
Log.i("onSaveInstanceState which_dialog", "which_dialog is null");
}
}
onRestoreInstanceState
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("which_dialog")) {
if (savedInstanceState.getString("which_dialog").equals("complete")) {
complete_dialog = new CompleteDialog(Activity.this);
imageFileForCamera_ = (File)savedInstanceState.getSerializable(INSTANCE_STATE_IMAGE_FILE_FOR_CAMERA);
}
}
} else {
Log.i("onRestoreInstanceState", "savedInstanceState is null");
}
}
答案 0 :(得分:0)
您正在which_dialog
中保存onSaveInstanceState()
。您无法在which_dialog
中恢复onRestoreInstanceState()
。根据您在onRestoreInstanceState()
中保存的值修改which_dialog
以设置onSaveInstanceState()
。