java - 在 Android 上,带有 fragment 的 Save-Bundle 不保存状态

标签 java android android-fragments

我有一个 Android 应用程序,它有两个 fragment ,将数据从 fragment A 传递到 fragment B。我可以正确加载所有数据,但是当我更改屏幕方向时,所有数据都会重新加载。我已经在要保留状态的 fragment 中设置了 onSaveInstanceStateonViewStateRestored 方法,但当屏幕方向发生变化时,什么都不会保留。

需要解决的主要问题是当文本加载到 FragmentB 中的 TextView 时 - 当屏幕方向更改时它需要保留在那里。

MainActivity.Java

package com.example.tourguide;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
    private FragmentA fragmentA;
    private FragmentB fragmentB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentA = new FragmentA();
        fragmentB = new FragmentB();

        if(savedInstanceState == null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container_a, new FragmentA());
            ft.replace(R.id.container_b, new FragmentB());
            ft.commitNow();
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

    }

    @Override
    public void onInputASent(CharSequence input) {
        fragmentB.updateEditText(input);
    }

    @Override
    public void onInputBSent(CharSequence input) {
        fragmentA.updateEditText(input);
    }
}

FragmentA.Java

package com.example.tourguide;

import android.content.Context;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.ListFragment;


public class FragmentA extends ListFragment implements AdapterView.OnItemClickListener {
    private FragmentAListener listener;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String[] locationList = getActivity().getResources().getStringArray(R.array.location_list);
        updateEditText(locationList[position]);
        listener.onInputASent(locationList[position]);
    }

    public interface FragmentAListener {
        void onInputASent(CharSequence input);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_a, container, false);

        return v;
    }


    @Override
    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    public void updateEditText(CharSequence newText) {
        //editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentAListener) {
            listener = (FragmentAListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement FragmentAListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.location_list, android.R.layout.simple_list_item_1);

        setListAdapter(arrayAdapter);

        getListView().setOnItemClickListener(this);
    }

}

FragmentB.Java - 这是屏幕旋转时需要将字符串保留在 TextView 内的 fragment

package com.example.tourguide;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class FragmentB extends Fragment {
    private FragmentBListener listener;

    private TextView resultsFrag;

    private static final String KEY_TITLE = "title_key";

    private String stateObject;

    public interface FragmentBListener {
        void onInputBSent(CharSequence input);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_b, container, false);

        resultsFrag = v.findViewById(R.id.tvFragmentB);

        //I used the same if block in three different locations and none of them worked
        if (savedInstanceState != null) {
            String itemTo = savedInstanceState.getString(KEY_TITLE);
            resultsFrag.setText(itemTo);
        } else {
            Toast.makeText(getActivity(), "Error 1", Toast.LENGTH_SHORT).show();
        }

        return v;
    }

    public void updateEditText(CharSequence newText) {

        String newOption = newText.toString();
        stateObject = newText.toString();

        Resources res = getResources();
        String[] locationDesc = res.getStringArray(R.array.location_description);

        if (newOption.equals("Calgary")) {
            resultsFrag.setText(locationDesc[0]);
        } else if (newOption.equals("Victoria")) {
            resultsFrag.setText(locationDesc[1]);
        } else if (newOption.equals("Vancouver")) {
            resultsFrag.setText(locationDesc[2]);
        } else if (newOption.equals("Toronto")) {
            resultsFrag.setText(locationDesc[3]);
        }

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentBListener) {
            listener = (FragmentBListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement FragmentBListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_TITLE, resultsFrag.getText().toString());
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);

        //I used the same if block in three different locations and none of them worked
        if (savedInstanceState != null) {
            String itemTo = savedInstanceState.getString(KEY_TITLE);
            resultsFrag.setText(itemTo);
        } else {
            Toast.makeText(getActivity(), "Error 2", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //I used the same if block in three different locations and none of them worked
        if (savedInstanceState != null) {
            String itemTo = savedInstanceState.getString(KEY_TITLE);
            resultsFrag.setText(itemTo);
        } else {
            Toast.makeText(getActivity(), "Error 3", Toast.LENGTH_SHORT).show();
        }
    }

}

fragment_b.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_light"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/tvFragmentB"
        android:layout_width="match_parent"
        android:layout_height="602dp" />

</LinearLayout>

字符串

<resources>
    <string name="app_name">Tour Guide</string>

    <string-array name="location_list">
        <item>Calgary</item>
        <item>Victoria</item>
        <item>Vancouver</item>
        <item>Toronto</item>
    </string-array>

    <!-- Descriptions for the locations -->
    <string-array name="location_description">
        <item>This is a big description for CALGARY - THIS needs to load in for more information regarding CALGARY</item>
        <item>This is a big description for VICTORIA - THIS needs to load in for more information regarding VICTORIA</item>
        <item>This is a big description for VANCOUVER - THIS needs to load in for more information regarding VANCOUVER</item>
        <item>This is a big description for TORONTO - THIS needs to load in for more information regarding TORONTO</item>
    </string-array>

</resources>

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_green_light"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="206dp" />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/container_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/container_b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

最佳答案

它们已保存,但您正在替换它们。使用这个

if(savedInstanceState == null) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.container_a, new FragmentA());
    ft.replace(R.id.container_b, new FragmentB());
    ft.commit();
}

请注意, fragment 实例化几乎是此检查产生正确且预期结果的唯一场景。

不要保留对 Fragment 的引用,除非它是通过 findFragmentByTag + 实例化获得的。在这种情况下,您可能需要使用 commitNow 来获得可靠的结果。

<小时/>

编辑:更新为更完整的代码:

public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
    private FragmentA fragmentA;
    private FragmentB fragmentB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(savedInstanceState == null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container_a, new FragmentA(), "fragmentA");
            ft.replace(R.id.container_b, new FragmentB(), "fragmentB");
            ft.commitNow();
        }

        fragmentA = getSupportFragmentManager().findFragmentByTag("fragmentA");
        fragmentB = getSupportFragmentManager().findFragmentByTag("fragmentB");
    }

关于java - 在 Android 上,带有 fragment 的 Save-Bundle 不保存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60660909/

相关文章:

android - 自动缩放文字大小

Android - 如何显示包含从 fragment 中获取的字符串的 snackbar

java - 是否可以在 Java 中将 BigInteger 乘以 Double?

java - Java 中 ArrayList 的合并排序

java - 使用 servlet,如何从数据库下载多个文件并将它们压缩以供客户端下载

java - Android ListPreference不保存选择

安卓推特应用 : suspended (exception( Twitter Exception))

java - Android - 获取从文件资源管理器中选择的.txt文件的真实路径

java - 为什么我的 fragment 在我的 View 页面中重新初始化?

java - getMapAsync 错误