我如何正确地在活动和片段之间进行交互 [英] How can i correctly interact between Activities and fragments

查看:58
本文介绍了我如何正确地在活动和片段之间进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用步进器.所以目前我有一个带有3个片段的活动,用户必须在其中填写一些信息,例如表格. 有很多信息,所以我做了4个类来分隔该信息. 此外,一些信息是自动获取的,因此在片段中我会请求权限... 例如:

I'm working with Steppers. So currently i have one Activity with 3 Fragments in which the user must complete with some information, like a Form. There are a lot of information so i made 4 classes to separate that information. In addition, some information is got it automatically so in fragments i ask for permissions... For Example:

public class UserIds {
@Nullable
@Expose  String phone;
@Expose String Email;
@Expose String phone2;
@Expose String ModCel;
@Expose String Doc;


//Setters, getters and another simple method

public class UserLocation {
@Nullable
@Expose  String street;
@Expose int number;
....

//Setters, getters and another simple method

...

以此类推,还有2个类.

And so on with 2 classes more.

所以,正如您所看到的,我也在进行改造.

So, as you can see i'm working with retrofit too.

我该如何正确处理类似的事情? 我读到有关 Parceler ,接口,EventBus的信息. 我应该在Activity中声明所有对象实例,然后在每个片段中进行修改(某些对象由不同的片段进行修改)还是在每个片段中创建实例,存储信息,并在按下完成"按钮时获取信息?在OnDestroy()调用的情况下如何保存该对象?

How can I correctly handle something like that? I read about Parceler , Interfaces, EventBus... Should I declare all objects instances in the Activity and then modify in each fragment ( Some objects are modified by differents fragments) or maybe create instances in each fragment, store the information and in when the Complete button is pressed, obtain the information? How should i save this objects in case of OnDestroy() call?

要考虑的另一件事是,最后,当表单结束时.其他活动可能拥有所有信息并要求更多信息(是的,需要大量信息).

Another things to take into account is that finally, when the form is end. Other activity may have all the information and ask for more (yeah, a LOT OF INFORMATION IS NEEDED).

最后,每次用户填写表单时(使用完成"按钮,然后在其他活动要求更多信息时,此数据将发送到服务器)

Finally, every time the user complete the form (with the complete button and then when the other activity ask for more, this data is sended to the server)

推荐答案

我选择了Parceler方式,并且运行良好.也许可以帮助别人,我将@Parcel放在每个POJO类中,然后由于我要在片段中使用StepperAdapter处理片段(由于阶梯石库),因此我想保存数据:

I chose the Parceler way and work perfectly. Maybe help somebody, i put @Parcel in each POJO class, then as i am handling with fragments with StepperAdapter (because of stepstone library) in the fragment which i want to save data i did this:

    // Obtaing all the fragments 
    List<Fragment> steps = getFragmentManager().getFragments();
    // save states
    Bundle bundle = new Bundle();
    Parcelable wrapped = Parcels.wrap(obj1);
    Parcelable wrapped2 = Parcels.wrap(obj2);
    bundle.putParcelable("OBJ1", wrapped);
    bundle.putParcelable("OBJ2", wrapped2);
    steps.get(fragment2reference).getArguments().putAll(bundle);

然后在接收的片段中,您必须创建一个构造函数,然后您才能接收数据(因为已经创建了片段,因此发生了包抛出错误)

Then in the fragment that receive, you have to create a constructor and then you can receive the data (because of fragment was already created, so the bundle throw error)

//Constructor
public fragment2(){
    super();
    setArguments(new Bundle());
}

fragment2显示时:

When fragment2 shows :

   OBJ1 a = Parcels.unwrap(getArguments().getParcelable("OBJ1"));
   OBJ2 b = Parcels.unwrap(getArguments().getParcelable("OBJ2"));

希望有人帮助!

这篇关于我如何正确地在活动和片段之间进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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