如何通过活动之​​间的信息 [英] How to pass information between Activities

查看:200
本文介绍了如何通过活动之​​间的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也问过这个问题了几次就到这里了,但我仍然没有足够的信心,开始扔东西到我的code,因为我怕我会毁了它已经拥有的功能。

I have asked this question a few times on here already, but still am not confident enough to start throwing things into my code because I'm afraid I'll ruin the functionality it already has.

我想做的事就是有,我可以发送/接收来自数据/,并能够从几个不同的活动访问它的设备的蓝牙连接。我知道意图可以使用,但任何人都可以尝试解释意图是如何工作的?

What I want to do is have a Bluetooth connection with a device that I can send/receive data to/from, and be able to access it from several different Activities. I know Intents can be used, but can anyone try to explain how intents work?

我不明白,如果我开始与一个活动,我怎么可以在活动中建立一个对​​象,然后结束它,并且仍然有对象。当我打电话的意图在下面的活动,我该如何访问它?

I don't understand how if I start with an Activity, how I can build an object within that Activity, then end it, and still have the object. When I call the intent in the following Activity, how do I access it?

我无法找到一个通过一个意图通过自定义对象,在另一个活动开,然后通过另一个活动再次通过,因此在一个活动的例子code。所有我能找到的东西是字符串的,诚信的,似乎这些方法都是针对特定对象硬codeD。

I can not find example code of an Activity with a custom object that is passed through an intent, opened in another Activity, then passed again through another Activity, and so on. All I can find is things for string's, int's and it seems that those methods are hardcoded for the specific objects.

任何帮助将大大AP preciated,谢谢。

Any help would be greatly appreciated, thanks.

推荐答案

有你没有找到一份转好例子复杂对象的原因是:你不认为到了很多。它可能意图额外的任何物体转移,与申报要传送为实现Serializable,或Parcalable类。

there is a good reason you don't find a good example of transfering complicate objects: you don't suppose to that a lot. it's possible to transfer with intent extra any object, with declare the class you want to transfer as implements Serializable, or Parcalable.

但是 - 这是不推荐这样做,除非你有很好的理由。
有无限的方式打造跨活动的对象(数据库,singeltones,服务..),并通过复杂的对象意图是沉重的操作。同时它使得可怕的表演这么做。

but - this is not recomended doing so, unless you have good reason. there are unlimited ways to create "cross activities" objects (databases, singeltones, services..), and passing complicated objects in intent is heavy operation. also it makes terrible performances doing so.

反正 - 如果你想反正这样做,这是方式:

anyway - if you want anyway doing so, that's the way:

这是如何传递serlizable对象:

this is how to pass serlizable object:

Intent intent =  new Intent();
    intent.putExtra(name, someInstanceOfClassWhichImplementsSerializableInterface);

这是怎么acheive它的其他活动:

this is how to acheive it on the other activity:

getIntent().getSerializableExtra(name);

有关您不了解如何对象仍居住在完成后该活动的问题 - 完成()活动没有触发牵引!!!,但做触发器中,以onDestoy()回调 。它不是从Intent对象preventing活路 - - 因为没有人说你的活动是国内唯一一家持有提到它时,它是正确的时间调用活动distracor,即使它是牵引系统决定。系统为它传递到它打开了新的活动的举办perpose的意图参考。

about the question that you don't understand how the object still lives after you finish the activity - finish() activity is not triggering distractor!!!, but do triggers to onDestoy() callback. the system decides when it's the right time call the activities distracor , and even if it was distractor - it's not preventing from the Intent object to stay alive - because nobody said your activity is the only one holding reference to it. the intent reference held by the system for the perpose of passing it to the new activity which opened with it.

这篇关于如何通过活动之​​间的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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