简体   繁体   English

Android Bundle是否共享内存?

[英]Android Bundle is shares memory?

I using setArgument() / getArgument() for passing data from one fragment to another fragment. 我使用setArgument()/ getArgument()将数据从一个片段传递到另一个片段。

I stored ArrayList into bundle using Bundle.putParcelableArrayList(), and I founded they - putted data and get data - are equals on memory. 我使用Bundle.putParcelableArrayList()将ArrayList存储到bundle中,然后我发现它们-放入数据和获取数据-等于内存。

Generally, I think, Parcelable source and regenerated(through CREATOR) instance is another instance, but they are same. 通常,我认为,可打包源和通过CREATOR再生的实例是另一个实例,但是它们是相同的。

If Bundle shares memory, Why bundle has several methods for supports variety datatypes- Why not just passing Object instance or using generic on Bundle? 如果Bundle共享内存,那么为什么bundle有几种支持多种数据类型的方法-为什么不仅仅传递Object实例或在Bundle上使用泛型?

Why bundle has several methods for supports variety datatypes- Why not just passing Object instance or using generic on Bundle? 为什么bundle有几种支持多种数据类型的方法-为什么不仅仅传递Object实例或在Bundle上使用泛型?

If the Bundle class would had only 1 method to pass objects , that would be horrible because that might cause confusion. 如果Bundle类只有一种方法可以传递objects ,那就太可怕了,因为这可能会引起混乱。 Those variety datatypes method help the programmer to be sure of the kind of argument he/she want to get or put, Imagine this case where 4 programmers are working on a project and 1 programmer want to put an object in the Bundle instance (in this example imagine that the Bundle class has only 1 method to put objects (polymorphism)) 这些多样化的数据类型方法可以帮助程序员确定他/她想要获取或放入的参数的种类。试想一下这种情况,其中有4位程序员在一个项目上工作,而有1位程序员想要在Bundle实例中放入一个对象(在本例中例如,假设Bundle类只有一种方法放置对象(多态)

Bundle bundle = new Bundle();
bundle.putObject("animal",new Dog());

And then later one of the programmer want to get that value and do this: 然后,一位程序员想要获取该值并执行此操作:

Bundle bundle = getArguments();
Cat variable = bundle.getObject("animal"); //This will cause an Exception, because the     argument it returned isn't a Cat class.

That's why Bundle class has those methods, to avoid this kind of errors. 这就是为什么Bundle类具有这些方法以避免此类错误的原因。

Now the next answer is for the 现在下一个答案是

or using generic on Bundle? 或在Bundle上使用通用?

Generics was made to avoid the use of Casting, and to help programmers with the Collection Framework to avoid the kind of problem i described above. 泛型是为了避免使用Casting,并帮助程序员使用Collection框架来避免上述问题。

public final class Bundle implements Parcelable, Cloneable {...} 

Bundle implements Parcelable, the system will automatically identify whether Bundle is a cross-process and cross-process if it is not, then the data is passed directly through shared memory, so this time you pass an object with the object to get is actually the same, but if it is cross-process, Parcelable interfaces to play a role in the cross-process, the system will call Parcelable's writeToParcel method, all data are converted into bytes, and then passed on to other processes, other processes received after, and then through the Parcelable Creator interface to restore data, restore data by Creator certainly is not the same instance. Bundle实现了Parcelable,系统将自动识别Bundle是否为跨进程,如果不是,则将数据直接通过共享内存传递,因此这次您将一个对象与该对象一起传递实际上是相同,但是如果是跨进程的,则Parcelable接口在跨进程中发挥作用,系统将调用Parcelable的writeToParcel方法,将所有数据转换为字节,然后传递给其他进程,之后再接收其他进程,然后通过Parcelable Creator界面还原数据,通过Creator还原数据肯定不是同一实例。 As for why there are so many ways Bundle, rather than directly using putObject (Object), getObject (), partly because Parcelable is required, not every type can parcel, then Bundle since realized Parcelable, must also be he needs to pass bundled data requirements are also supported parcel 至于为什么有这么多的捆绑方式,而不是直接使用putObject(Object),getObject(),部分是因为需要Parcelable,不是每个类型都可以捆绑,所以Bundle既然实现了Parcelable,还必须是他需要传递捆绑数据要求也支持包裹

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM