ViewPager中多个片段之间的通信对象 [英] Communication objects between multiple fragments in ViewPager

查看:80
本文介绍了ViewPager中多个片段之间的通信对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ViewPager中有5个片段,用于逐步用几个字段填充业务对象,在每个步骤中,将设置其中一些字段.我已经阅读了许多有关片段之间通信的文章,但是我不喜欢别人喜欢的方式,因此在考虑了如何在我的情况下执行此操作之后,终于开始考虑使用所有片段都可以轻松访问的单例模型对象并在特定步骤中填充它们.

I have 5 fragments in ViewPager used to fill business object with several fields step by step, in each step some of those fields will be set. I've read many articles about communication between fragments but I'm not feeling comfortable the way others preferred, so after thinking about HOW should I do this in my case, finally I start thinking to use singleton model object which all fragments can easily access to its fields and fill them in specific steps.

由于我是android的新手,我想听听专家关于使用singleton而不是在片段之间(例如已实现的接口)传递数据的消息(这看起来是如此复杂且难以维护).任何建议都会有所帮助.

As I'm new to android I want to hear from experts about using singleton instead of passing data between fragments such as implemented interface(It seems its so complicated and hard to maintenance). Any advice will be helpful.

推荐答案

虽然单例方法似乎易于实现并且理解,但这并不是最好的方法来实现所需的方法.原因之一是您的模型对象或您所说的业务对象位于活动上下文之外,这可能会导致难以发现错误.例如.如果系统创建了一个以上活动类的实例,并且两个实例都引用了您的单例.看看您如何失去对物体的追踪?

While singleton approach seems easy to implement and understand it is way not to best way to achieve what you need. One reason is that your model object or as you call it business object lives outside of your activity's context which can create hard to find bugs. E.g. in case when more than one instance of your activity class is created by system and both keep reference to your singleton. See how you lose track of your objects?

我要做的是

  1. 让我的模型对象实现 Parcelable 一开始就讨厌它,但是一旦习惯了它,它将成为您的模特最好的朋友
  2. 由于您的模型为parcelable,现在您可以轻松地在片段,活动之间传递它,甚至可以将其保存在共享首选项中. 在片段或活动之间传递parcelable时要注意的一件事,就像按值传递,即每次创建新实例一样.
  3. 设置片段的参数,或者如果它已被实例化,则获取参数并添加模型.这是一个例子: 如果某个片段尚未激活:

  1. Make my model object to implement Parcelable you will hate it at the beginning but once you get use to it it will become your model's best friend
  2. Since your model is parcelable now you can easily pass it between fragments, activities, and even save it in shared preferences. One important thing to note here when you pass your parcelable between fragment or activity it is like pass by value, i.e. every time new instance is created.
  3. Set your fragment's argument or if it is already instantiated then get arguments and add your model. here is an example: if a fragment is not active yet:

Bundle args = new Bundle(); args.putParcable("businessObject", yourBusinessObjectThatIsParcable); yourFragment.setArguments(args);

Bundle args = new Bundle(); args.putParcable("businessObject", yourBusinessObjectThatIsParcable); yourFragment.setArguments(args);

否则: yourFragment.getArguments().putParcelable("businessObject", yourBusinessObjectThatIsParcable);

也许在片段中的onCreateView方法中,像这样获得模型对象MyParcableObject mpo = (MyParcableObject)getArguments().getParcelable("businessObject"),并使用它设置所需的任何数据.

In your fragment perhaps in onCreateView method get your model object like this MyParcableObject mpo = (MyParcableObject)getArguments().getParcelable("businessObject") and use it set whatever data you want.

在单击按钮或在onPause方法中完成对象编辑后,以相同的方式更新片段的参数getArguments().putParcelable("businessObject", mpo);

When you finish editing your object on button click or in onPause method updated your fragment's arguments same way getArguments().putParcelable("businessObject", mpo);

在您的最后一页或最后一个片段中,您可以将对象传递到活动中,此处是如何做到

in your last page or last fragment you can pass your object to your activity, here is how to do it

尽管看起来很麻烦,但这是您需要习惯作为Android开发人员的一种做法.当模型实现parcelable时,您将获得更多的控制权.

Even though it looks cumbersome but it is a practice that you need to get used to as an android developer. You get lot more control when your model implements parcelable.

另一种满足您需求的方法是通过委派模式,但它通常用于回调,即使您也可以传递对象.

Another way to do what you need is thru Delegation Pattern but it is mostly used for callbacks even though you can pass objects as well.

这篇关于ViewPager中多个片段之间的通信对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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