如何发送通过捆绑对象 [英] How to send objects through bundle

查看:123
本文介绍了如何发送通过捆绑对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要传递一个对不通过捆绑我的大部分处理的类。

I need to pass a reference to the class that does the majority of my processing through a bundle.

现在的问题是它没有任何关系的意图或背景,并有大量的非基本对象。如何将类打包到parcelable /序列化,并将其传递给 startActivityForResult

The problem is it has nothing to do with intents or contexts and has a large amount of non-primitive objects. How do I package the class into a parcelable/serializable and pass it to a startActivityForResult?

推荐答案

搞清楚什么道路采取需要回答不仅CommonsWare的关键问题:为什么也是这个问题:什么?你过去的。

Figuring out what path to take requires answering not only CommonsWare's key question of "why" but also the question of "to what?" are you passing it.

现实情况是,可以通过捆绑的仅仅是简单的数据 - 一切基于跨$ P $的数据意味着或指向什么ptations。你不能从字面上传递一个对象,但你可以做的是以下三种情况之一:

The reality is that the only thing that can go through bundles is plain data - everything else is based on interpretations of what that data means or points to. You can't literally pass an object, but what you can do is one of three things:

1)可以打破对象向下到其构成的数据,并且如果有什么的另一端具有相同的排序对象的知识,也可以从序列化的数据装配的克隆。这就是最常见的类型通过捆绑。

1) You can break the object down to its constitute data, and if what's on the other end has knowledge of the same sort of object, it can assemble a clone from the serialized data. That's how most of the common types pass through bundles.

2)你可以传递一个不透明的句柄。如果你在同一范围内传递给它(尽管有人可能会问,为什么打扰),这将是一个手柄,你可以调用或间接引用。但是,如果你通过粘合剂把它传递给不同的环境中它的字面值将是一个任意数量(事实上,这些任意数字顺序从启动计数)。你不能做任何事情,但跟踪它,直到你通过它回到了原来的背景下,这将导致活页夹回到它改造成原来的手柄,使得这又是有用的。

2) You can pass an opaque handle. If you are passing it within the same context (though one might ask why bother) that will be a handle you can invoke or dereference. But if you pass it through Binder to a different context it's literal value will be an arbitrary number (in fact, these arbitrary numbers count sequentially from startup). You can't do anything but keep track of it, until you pass it back to the original context which will cause Binder to transform it back into the original handle, making it useful again.

3)您可以通过一个神奇的手柄,如文件描述符或引用某些OS /平台的对象,如果设置正确的标志活页夹将创建克隆指向同一资源的收件人,可实际可以使用上的另一端。但是,这仅适用于极少数类型的对象。

3) You can pass a magic handle, such as a file descriptor or reference to certain os/platform objects, and if you set the right flags Binder will create a clone pointing to the same resource for the recipient, which can actually be used on the other end. But this only works for a very few types of objects.

最有可能的,你要么通过你的类只让另一端可以跟踪它,并给它回到你以后,或者你通过它到克隆可以从序列构成的数据创建的背景下.. 。要不然你正在尝试做的东西,仅仅是行不通的,你需要重新思考整个方法。

Most likely, you are either passing your class just so the other end can keep track of it and give it back to you later, or you are passing it to a context where a clone can be created from serialized constituent data... or else you are trying to do something that just isn't going to work and you need to rethink the whole approach.

这篇关于如何发送通过捆绑对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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