【问题标题】:Fragment's bundle is null in onCreate despite overriding onSaveInstanceState尽管覆盖了 onSaveInstanceState,但片段的包在 onCreate 中为空
【发布时间】:2018-12-04 13:05:20
【问题描述】:

我的 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 标签来添加 Fragment。

标签: android android-fragments android-bundle startactivityforresult android-savedstate


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2013-08-19
    • 2015-05-25
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    相关资源
    最近更新 更多