android - 尽管覆盖了 onSaveInstanceState,但片段的包在 onCreate 中为空
问题描述
我的 MainActivity 使用 Fragments,布局的简化版本如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:name="com.lafave.MyFragment1"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<View
android:layout_width="@dimen/divider_width"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
<fragment
android:name="com.lafave.MyFragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在 Fragment 中,我使用以下代码启动本机相机应用程序:
mRecentPhotoPath = file.getAbsolutePath();
final Uri uri = Uri.fromFile(file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
我的 Fragment 的 onActivityResult 方法取决于保留的 mRecentPhotoPath 的值:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//mRecentPhotoPath is used here to display the photo.
}
}
但是,如果我在本机相机应用程序运行时旋转设备,则会创建我的 Fragment 的新实例,并且不会保留 mRecentPhotoPath。我想我可以通过在 Fragment 中实现 onSaveInstanceState 来解决这个问题,如下所示:
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if(mRecentPhotoPath != null) {
outState.putString(RECENT_PHOTO_PATH_ARUGMENT, mRecentPhotoPath);
}
}
但是,即使我将状态保存到包中,当 Fragment 恢复时,onCreateView、onActivityCreated 和 onViewStateRestored 方法的包始终为 null。我究竟做错了什么?
实际上,无论使用哪种相机,这似乎都是一个问题。如果我旋转我的应用程序(没有打开本机相机),那么在 onCreateView 等各种方法中,Bundles 始终为空。
解决方案
谢谢@EpicPandaForce,你让我走上了正确的道路。问题是因为我的 MainActivity 的布局没有在 Fragment 上使用 id。进行此更改解决了我的问题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<fragment
android:id="@+id/myFragment1"
android:name="com.lafave.MyFragment1"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<View
android:layout_width="@dimen/divider_width"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
<fragment
android:id="@+id/myFragment2"
android:name="com.lafave.MyFragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
推荐阅读
- sql - SQL(雪花) - 我如何从第一个结果中返回 1 行或在第二个连接中使用 MAX
- python-3.x - standard_init_linux.go:228: exec 用户进程导致:操作不允许
- javascript - 如何使用取自文本字段的片段构建链接,但在 JavaScript 中以随机化它们,然后在其自己的选项卡上打开每个片段
- r - 使用“$”并在 R 中的一行中运行函数的好习惯
- android - VS for Mac 更新后不编译 Xamarin Android 应用程序
- gnome-3 - 如何启用缩略图
- php - 为什么在新的 xampp 安装 php 后 Json 编码返回错误?
- java - SpringCloudStream 与 Confuent 模式注册表
- java - preparedstatement.executeupdate() 中的错误
- react-native - 即使应用程序未运行或关闭,如何从 react-native-push-notification 获取通知