15

我正在尝试找出在不破坏“空构造函数”规则的情况下将 Object 传递给FragmentDialogFragment的正确方法。

例如,我创建了一个自定义视图,并且对于我实例化的每个视图,我都想关联一个 DiaglogFragment。此 DialogFragment 将用于显示控件,用户可以使用这些控件更改与其关联的自定义 View 的某些方面。因为 View 是我理解的对象,所以我不能使用setArguments()

我可以实现我的 DialogFragment 即工厂模式的 newInstance(View) 方法,但是如果我的 Fragment 被系统保存然后在以后恢复会发生什么?据我所知,不会有对 View 对象的引用?

有人可以告诉我我是否以错误的方式使用片段,或者有没有办法实现将对象传递给片段,这也将处理系统在以后重建它。

4

4 回答 4

16

在您的DialogFragmnet课程中,您创建了两个方法:

  1. newInstance做你的实例DialogFragment

  2. 设置器来初始化你的对象

setRetainInstance(true);并添加onCreate

public class YourDialogFragment extends DialogFragment {

    ComplexVariable complexVar;

    public static YourDialogFragment newInstance(int arg, ComplexVariable complexVar) {
        YourDialogFragment frag = new MoveSongDialogFragment();
        Bundle args = new Bundle();
        args.putInt("count", arg);
        frag.setArguments(args);
        frag.setComplexVariable(complexVar);
        return frag;
    }

    public void setComplexVariable(ComplexVariable complexVar) {
        this.complexVar = complexVar;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
}

然后,显示对话框

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();

Fragment prev = manager.findFragmentByTag("yourTag");
if (prev != null) {
    ft.remove(prev);
}

// Create and show the dialog.
DialogFragment newFragment = YourFragmentDialog.newInstance(argument, yourComplexObject);
newFragment.show(ft, "yourTag");
于 2014-05-02T19:50:30.560 回答
3

您可以将 Bundle extras 中的 Object 作为 Parcelable Objects ( http://developer.android.com/reference/android/os/Parcelable.html ) 传递,并将它们传递给onCreateView(Bundle savedInstanceState). 如果用户翻转屏幕,您可以保存它们。

编辑: 这个Parcelable 教程非常好!

另一种方法是从您的 ParentActivity 获取数据对象,但我不确定这是否是一个好方法(但它有效..)

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mYourObject = ((MainActivity) getActivity()).getYourObject();
}

您必须为此在 Activity 中创建一个 Getter

public YourObject getYourObject(){
   return mYourObecjt;
}

但我想 Parcelables 是更好的方法,因为您可以在没有任何依赖关系的情况下重用您的片段......

于 2013-05-06T19:43:20.853 回答
0

你总是可以使用像 GSON 或Genson这样的优秀JSON库来序列化对象——这是我对任何远程复杂对象的 goto 方法。对于简单的操作,GSON 稍微紧凑一些,但是如果您的视图中有任何类型的继承/多态性,我强烈推荐 Genson。只要你记录它们,你就可以有一个带参数的 ctr。

https://code.google.com/p/genson/ - 在下载部分获取 JAR。

于 2013-05-07T12:38:22.660 回答
0

我通过共享偏好解决了我的需求。YuzdeDataUser 是我的自定义类,我从我的 Listview 中获取它 onClickItemListener

如何发送

  YuzdeDataUser clickedObj = (YuzdeDataUser) parent.getItemAtPosition(position);
            //clickedObj.setTarihim();
            SharedPreferences shp = getSharedPreferences("sam", MODE_PRIVATE);
            SharedPreferences.Editor editor = shp.edit();

            Gson gson = new Gson();
            String json = gson.toJson(clickedObj);
            editor.putString("yoklamaList", json);
            editor.commit();

            FragmentManager fragmentManager = getSupportFragmentManager();
            AylikRaporPop userPopUp = new AylikRaporPop();
            userPopUp.show(fragmentManager, "sam");

如何接收

    SharedPreferences shp = parent.getSharedPreferences("sam", MODE_PRIVATE);
    SharedPreferences.Editor editor = shp.edit();

    Gson gson = new Gson();
    String json = shp.getString("yoklamaList", "");
    YuzdeDataUser user = gson.fromJson(json, YuzdeDataUser.class);
    if (user != null) {
        txt_ad.setText(user.getAd());
        txt_tur_tarih.setText(Util.getInstance(parent).writeNameOfTur(user.getTur_id()));
        txt_var.setText("" + user.getVar());
        txt_gorevli.setText("" + user.getGorevli());
        txt_yok.setText("" + user.getYok());

    }
于 2017-07-12T09:13:56.930 回答