我正在使用startActivityForResult()
从相机中捕捉图像。在使用开发人员选项“不要保留活动”进行测试时,我注意到我的视图状态正在onViewStateRestored
中正确恢复,但在onActivityResult
被调用之前会再次丢失。看起来正在创建同一片段的第二个实例;有两次拨打onCreate
,一次有效savedInstanceState
,另一次有savedInstanceState == null
。第一个永远不会到达onResume
。
这就是我在日志中看到的内容:
E/CaptureFragment﹕ fireTakePictureIntent: tempPhotoPath: /storage/emulated/0/Pictures/JPEG_2014-10-15 23.19.45.jpg
E/CaptureFragment﹕ startActivityForResult: tempPhotoPath: /storage/emulated/0/Pictures/JPEG_2014-10-15 23.19.45.jpg
I/am_on_paused_called﹕ [0,com.chimbori.galapagos.views.capture.CaptureActivity]
E/CaptureFragment﹕ onSaveInstanceState: tempPhotoPath: /storage/emulated/0/Pictures/JPEG_2014-10-15 23.19.45.jpg
E/CaptureFragment﹕ onCreate: tempPhotoPath: /storage/emulated/0/Pictures/JPEG_2014-10-15 23.19.45.jpg
E/CaptureFragment﹕ onViewStateRestored: tempPhotoPath: /storage/emulated/0/Pictures/JPEG_2014-10-15 23.19.45.jpg
E/CaptureFragment﹕ onCreate: tempPhotoPath: null
E/CaptureFragment﹕ onStart
E/CaptureFragment﹕ onActivityResult : tempPhotoPath: null
E/CaptureFragment﹕ onResume: tempPhotoPath: null
I/am_on_resume_called﹕ [0,com.chimbori.galapagos.views.capture.CaptureActivity]
这些方法很简单:
private String tempPhotoPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && savedInstanceState.containsKey(IntentConstants.EXTRA_PHOTO_FILE)) {
tempPhotoPath = savedInstanceState.getString(IntentConstants.EXTRA_PHOTO_FILE);
}
Log.e(TAG, "onCreate: tempPhotoPath: " + tempPhotoPath);
}
private void fireTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
tempPhotoPath = FilePaths.createTempPhotoPath().getAbsolutePath();
Log.e(TAG, "fireTakePictureIntent: tempPhotoPath: " + tempPhotoPath);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(tempPhotoPath)));
Log.e(TAG, "startActivityForResult: tempPhotoPath: " + tempPhotoPath);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (tempPhotoPath != null) {
Log.e(TAG, "onSaveInstanceState: tempPhotoPath: " + tempPhotoPath);
outState.putString(IntentConstants.EXTRA_PHOTO_FILE, tempPhotoPath);
}
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null && savedInstanceState.containsKey(IntentConstants.EXTRA_PHOTO_FILE)) {
tempPhotoPath = savedInstanceState.getString(IntentConstants.EXTRA_PHOTO_FILE);
Log.e(TAG, "onViewStateRestored: tempPhotoPath: " + tempPhotoPath);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Log.e(TAG, "onActivityResult : tempPhotoPath: " + tempPhotoPath);
}
}
有什么想法可以在onViewStateRestored
上正确恢复,但在onActivityResult
及更高版本中返回null?
答案 0 :(得分:0)
好的,我明白了。我正在CaptureFragment
中重新创建CaptureActivity
,即使我传递的是非空savedInstanceState
。这就解释了为什么要创建两个片段而不是一个片段。
修复前:CaptureActivity.onCreate
CaptureFragment captureFragment = new CaptureFragment();
getFragmentManager().beginTransaction()
.replace(R.id.activity_capture_content_frame, captureFragment,
CaptureFragment.class.getCanonicalName())
.commit();
修正:
CaptureFragment captureFragment = (savedInstanceState != null)
? (CaptureFragment) getFragmentManager().findFragmentByTag(CaptureFragment.class.getCanonicalName())
: new CaptureFragment();
getFragmentManager().beginTransaction()
.replace(R.id.activity_capture_content_frame, captureFragment,
CaptureFragment.class.getCanonicalName())
.commit();