首页 > 解决方案 > 尽管覆盖了 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 始终为空。

标签: androidandroid-fragmentsandroid-bundlestartactivityforresultandroid-savedstate

解决方案


谢谢@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>

推荐阅读