防止片段restoreViewState() [英] Preventing Fragment restoreViewState()

查看:83
本文介绍了防止片段restoreViewState()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象 Foo ,可以使用片段( FooFragment )对其进行配置.Foo类包含对FooFragment和方法 public Fragment getConfigurationFragment();的静态引用.此方法将当前对象分配给FooFragment并返回它.

I have an object Foo which can be configured using a fragment (FooFragment). The Foo class contains a static reference to FooFragment and the method public Fragment getConfigurationFragment(); this method assigns the current object to the FooFragment and returns it.

public class Foo{
    private static FooFragment fooFragment = new FooFragment();

    public Fragment getConfigurationFragment(){
        fooFragment.setObject(this);
        return fooFragment;
    }

    //various getters and setters
}

FooFragment大致如下:

FooFragment is roughly as follows:

public class FooFragment extends Fragment{
    private Foo f;

    private EditText field1, field2, etc;

    public void setObject(Foo f){
        this.f = f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //inflate view
        setupFieldListeners();
        //return view object
    }

    @Override
    private void onStart(){
        super.onStart();
        setupFields();
    }

    private void setupFields(){
        field1.setText(f.getField1());
        field2.setText(f.getField2());
        //etc
    }

    private void setupListeners(){
         field1.addTextChangedListener(new TextWatcher(){
             @Override
             public void afterTextChanged(Editable e){
                 f.setField1(e.getText().toString());
         });
             //Other empty necessary methods

         field2.addTextChangedListener(new TextWatcher(){
             //...
         });
    }
}

当我第一次在对象foo1上使用Fragment时,一切都很好.显示正确的信息,一切正常.

When I use the Fragment for the first time on object foo1, everything works great. The proper information is displayed and everything works.

当我第二次在另一个对象foo2上使用Fragment时,打开Fragment会将foo1中的所有属性( field1,field2等)写入foo2.我认为这是因为再次添加Fragment时,将运行restoreViewState()方法,该方法会将EditText字段的值更改为foo1的值,从而触发afterTextChanged()方法,并将foo1的值写入foo2.

When I use the Fragment for the second time on another object foo2, opening the Fragment causes all of the properties from foo1 (field1, field2, etc) to be written to the foo2. I believe that this is because when the Fragment is added again, the restoreViewState() method runs, which changes the values of the EditText fields to the values for foo1, causing the afterTextChanged() method to fire, and writing the values of foo1 into foo2.

我已尝试以下方法解决此问题:

I have tried the following to fix the problem:

-每当调用getConfigurationFragment()时创建一个新的FooFragment对象.这可行,但是我认为它不是最佳的,因为我知道避免在移动平台上不必要地创建对象是很好的.

-Creating a new FooFragment object whenever getConfigurationFragment() is called. This works, but I believe it's not optimal, as I understand that it's good to avoid needlessly creating objects on a mobile platform.

-覆盖Fragment中的onSaveInstanceState()并发送一个空Bundle.这是行不通的,因为在关闭Fragment时它似乎不会被调用.这是行不通的.

-Overriding onSaveInstanceState() in the Fragment and sending a null Bundle. This doesn't work as it doesn't look to be called when the Fragment is closed. It doesn't work.

-将setupFields()调用放在OnStart(),OnResume()和OnCreateView()中.这些都不起作用,因为当restoreViewState()运行时,它会掩盖foo2对象.

-Placing the setupFields() call in OnStart(), OnResume(), OnCreateView(). None of these work as when the restoreViewState() runs, it clobbers foo2 object.

-在onCreate()和onCreateView()中将Bundle设置为null.不起作用.

-Setting the Bundle to null in onCreate() and onCreateView(). Doesn't work.

我该怎么做才能将foo2信息加载到重用的Fragment中?

What can I do to get the foo2 information to load into the reused Fragment?

我对FragmentTransaction机制没有最好的了解.是否可以以某种方式告诉FragmentManager恢复视图状态?

I do not have the best understanding of the FragmentTransaction mechanism. Is it possible to somehow tell the FragmentManager to restore the View state?

在尝试从根本上做缺陷并避免使用时重用配置片段的想法吗?如果可以,为什么?

Is the idea of reusing a configuration Fragment as I'm trying to do fundamentally flawed and to be avoided? If so why?

还有其他魔法可以使它起作用吗?

Is there some other magic that will cause this to work?

提前感谢您的时间.

推荐答案

我在片段视图状态恢复方面也遇到了类似的困难.一种防止它的方法是在调用 restoreViewState()之前清除程序包专用的 mSavedViewState 字段:

I had a similar struggle with fragment view state restoration. One way to prevent it was to clear package-private mSavedViewState field before restoreViewState() is called:

package androidx.fragment.app;

import android.os.Bundle;
import android.view.View;

public class StatelessFragment extends Fragment {

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mSavedViewState = null;
    }
}

这篇关于防止片段restoreViewState()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆