gpt4 book ai didi

android - 查看状态在 onActivityResult 和 onResume 之间丢失

转载 作者:行者123 更新时间:2023-12-04 18:06:05 27 4
gpt4 key购买 nike

我正在使用 startActivityForResult() 从相机捕捉图像。在使用开发人员选项“不保留 Activity ”进行测试时,我注意到我的 View 状态正在 onViewStateRestored 中正确恢复,但在调用 onActivityResult 之前再次丢失.看起来正在创建同一 fragment 的第二个实例;有两次调用 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?有什么想法吗?

最佳答案

好的,我明白了。我在 CaptureActivity 中重新创建了 CaptureFragment,即使我收到的是非空 savedInstanceState。这就解释了为什么要创建两个 fragment 而不是一个 fragment 。

修复前: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();

关于android - 查看状态在 onActivityResult 和 onResume 之间丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26397340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com