java - 保留 fragment 数据

标签 java android android-fragments orientation

继我之前询问 implementing parceable objects 的帖子之后,我得到的解决方案是使用 setRetainInstance(true); 方法。

@CommonsWare 为我提供了 sample我修改如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="387dp"
        android:layout_height="103dp"
        android:text="New Text"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Text"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

RotationFragmentDemo.java

package com.rotationfragment;

import android.app.Activity;
import android.os.Bundle;

public class RotationFragmentDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This check returns 'null' for the first time when there are no fragments
        if (getFragmentManager().findFragmentById(android.R.id.content) == null) {

            getFragmentManager().beginTransaction().add(android.R.id.content, new RotationFragment()).commit();
        }
    }
}

RotationFragment.java

package com.rotationfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RotationFragment extends Fragment implements
        View.OnClickListener {

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState) {

        // This should help to retain fragment
        setRetainInstance(true);

        View result=inflater.inflate(R.layout.activity_main, parent, false);

        result.findViewById(R.id.button).setOnClickListener(this);

        textView = (TextView)result.findViewById(R.id.textView);

        return(result);
    }


    @Override
    public void onClick(View v) {

        textView.setText("Hello.");
    }
}

旋转后,TextView 中的文本不会恢复。

我知道你们中的一些人会建议使用 parceable writeString parceable 方法来修复这个简单的示例,但我想知道为什么这里没有使用 setRetainInstance(true) 保留 Fragment?

而且我不需要保留字符串,而是像上一篇文章中那样保留 SocketThreadActivity

最佳答案

请参阅answer ,“onRetainInstance保存了Fragment对象,但我仍然必须在onCreateView中重建UI”,所以你必须手动恢复 View 的状态

关于java - 保留 fragment 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574054/

相关文章:

java - 无效数据访问ApiUsageException : Unknown entity: UserWithRoles

java - TreeMap Key、Value 对存在,但 .get(Key) 返回值为 null

java - 将外部类添加到现有项目

android - ProgressDialog 中的 'indeterminate' 是什么意思?

android - 如何在启动 Activity 后在 Fragment 中的聚焦 EditText 中显示软键盘?

java - 如何从war打包的jar中读取PNG

java - 为 Google Play 以外的应用显示 Admob 广告

android - fragment 之间动画的共享元素

android - 在 Android ViewPager 上禁用滑动

java - 对如何从 txt 文件写入字符串然后将其转换为二维数组感到困惑