简体   繁体   English

Android:将数据从一个片段传递到另一个

[英]Android: passing data from one fragment To another

I wanted to pass arraylist of objects from one fragment to another i tried the following ways, wanted to know what are the pro's and con's of the following or is their any better approach 我想将对象的数组列表从一个片段传递到另一个片段,我尝试了以下方法,想知道以下内容的优缺点是什么,或者它们的更好方法是什么


First 第一

public myFragment(ArrayList<E> myarray) {
super();        
this.myarray=myarray;
}

In the above code i created a constructor in the fragment class and used it to modify the arraylist but as i read from net this is not the best practice i searched for other's 在上面的代码中,我在fragment类中创建了一个构造函数,并使用它来修改arraylist,但是当我从net读取时,这并不是我寻找其他方法的最佳实践


Second 第二

public static myFragment newInstance(ArrayList<E> myarray1) {
   //        TODO Auto-generated constructor stub
    MyFragment myFragment=new MyFragment();
    myarray=myarray1;

}

here i created a static method which modify's the static arraylist myarray 在这里,我创建了一个静态方法,用于修改静态数组列表myarray


Third 第三

public static myFragment newInstance(ArrayList<E> myarray1) {
   //        TODO Auto-generated constructor stub
    MyFragment myFragment=new MyFragment();
    Bundle bundle=new Bundle();
    bundle.putSerializable(TAG,myarray1);
    myFragment.setArguments(bundle);
    return myFragment;

}

In this code i created a Serializable arraylist and passed it to bundel then 在这段代码中,我创建了一个可序列化的数组列表,并将其传递给Bundel,然后

myarray=(ArrayList<E>) bundle.getSerializable(TAG);

retrived the arraylist from bundle 从包中检索arraylist


Fourth 第四

the forth method which i got on net was using parcelable instead of Serializable but it was bit difficult to create a parcelable, if any one can share easy way to create a parceable as i have array list inside arraylist. 我在网上得到的第四种方法是使用parcelable而不是Serializable,但是要创建一个parcelable有点困难,因为任何人都可以共享一种简单的方法来创建parceable,因为我在arraylist中有数组列表。


So which of them is best approach or is their any better approach to send custom object from one fragment to another and what are their pro's and con's 因此,从哪种片段中将自定义对象发送到另一片段中,哪种方法是最好的方法,或者哪种更好的方法是它们的优点和缺点?

1) You shouldn't ever override Fragment's constructor, it will cause unexpected crashes when system tries to recreate the fragment using the reflection. 1)您永远不要覆盖Fragment's构造函数,当系统尝试使用反射重新创建片段时,它将导致意外崩溃。

2) You shouldn't use static ArrayList to hold the data because if you run into situation where you want to instatiate two fragments of the same kind, one will override the data from the second one 2)您不应该使用静态ArrayList来保存数据,因为如果遇到要实例化两个相同种类的片段的情况,一个片段将覆盖第二个片段的数据

3) Serializable is acceptable way, but it's rather slow compared to the Parcelable since it uses reflection to restore your data 3)可Serializable是可以接受的方式,但与Parcelable相比它相当慢,因为它使用反射来还原数据

4) Parcelable is a way to go. 4)可Parcelable是一种方法。 It's a bit of pain to make your class Parcelable but it's way faster than option 3., and safer than options 1. and 2. 使您的类为Parcelable有点痛苦,但它比选择3更快,并且比选择1和2更安全。

another ideas: 另一个想法:

5) If you don't need to persist the data, you can construct a singleton class which will hold the data in the ArrayList , and you can access it from anywhere in the application 5)如果您不需要保留数据,则可以构造一个单例类,该类将把数据保存在ArrayList ,并且您可以从应用程序中的任何位置访问它

6) If you need to persist the data, you should store it in the database, and when you go from one Activity to antoher, or from one Fragment to another, you just pass the ID of the object in question and pull the data from DB. 6)如果需要保留数据,则应将其存储在数据库中,当您从一个Activity到另一个或从一个Fragment到另一个Fragment时,只需传递相关对象的ID并从中获取数据D B。

I would suggest using the newInstance() static method as this is the way that Google suggests that you create fragments (see http://developer.android.com/guide/components/fragments.html ) and make sure you have a no args constructor. 我建议您使用newInstance()静态方法,因为这是Google建议您创建片段的方式(请参阅http://developer.android.com/guide/components/fragments.html )并确保您没有参数构造函数。 (also limitations of others as Tomislav Novoselec has said) (正如Tomislav Novoselec所说的那样,也限制了其他人)

A nice way I've found to easily convert objects to implement the Parcelable interface is this Android studio plugin > android-parcelable-intellij-plugin . 我发现可以轻松地将对象转换为实现Parcelable接口的一种好方法是此Android Studio插件> android-parcelable-intellij-plugin There is also a website to help you do it too if you prefer > parcelable.com 如果您喜欢> parcelable.com,也有一个网站可以帮助您做到这一点

(Parcelable objects are also more efficient than serializable too I've been told, so use them were you can) (据我所知,可拆分对象也比可序列化对象有效,因此请尽可能使用它们)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:使用BroadcastReceiver将数据从一个片段传递到另一个片段 - Android: passing data from one fragment to another with BroadcastReceiver 将数据从一个片段传递到另一个 - Passing data from a Fragment to another one 将数据从一个片段传递到另一个片段 - Passing data from one fragment to another 将数据从一个片段传递到另一个片段时无法正常工作 - Passing data from one fragment to another is not working when passing data Java将数据从一个片段的Lis​​tView传递到另一个片段 - Java Passing Data From One Fragment's ListView To Another Fragment 空指针异常,用于将数据从一个片段传递到另一个片段 - Null pointer exception for passing data from one fragment to another fragment 在Android中将数据从一个片段传递到下一个片段 - Passing data from one fragment to next fragment in android 从一个片段传递到另一个 - Passing from one fragment to another 使用Android标签无法将信息从一个片段传递到另一个片段 - Passing info from one fragment to another using Android tabs not working 在Android的导航抽屉中从一个片段获取数据到另一个片段 - get data from one fragment to another fragment in navigation drawer in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM