意图和Parcelable对象的Andr​​oid [英] Intent and Parcelable object Android

查看:192
本文介绍了意图和Parcelable对象的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我需要我的包裹物,即使我只是需要将其发送给同一任务的另一个线程?其实我需要打开,将在同一线程(主线程)运行,即使活动。

Why do I need to parcel my object even if I just need to send it to another thread of the same task? Actually I need to open an activity that will run even on the same thread (the main thread).

在换句话说,为什么没有谷歌提供的版本startActivity那拿包的通用对象的广告parameteer,而不是让我开始一个活动的情况下,我知道这是在同一个进程中或(大部分时间)即使是同一个线程(主要的)?

In other words why didn't Google provide a version of startActivity that take a generic object ad parameteer instead of a bundle to let me start an activity in case I know it is within the same process or (most of the times) even the same thread (the main one)?

推荐答案

您不需要使用Parcelable从一个活动传递一个对象到另一个。你可以只存储一个参考对象的静态成员变量,像这样的:

You don't need to use Parcelable to pass an object from one activity to another. You can just store a reference to the object in a static member variable, like this:

public class Globals {
     public static MyObject myObject;
}

现在,在code具有对象,你刚刚做的:

Now, in the code that has the object, you just do:

Globals.myObject = object;

和在新的活动,你可以得到它是这样的:

and in the new activity, you can get it like this:

doSomethingWith(Globals.myObject);

现在,话虽如此,您需要注意以下事项:

Now, having said that, you need to be aware of the following:

Android的可以杀死你的过程中,如果您的应用程序在后台pretty的多就是了任何时候。当用户再返回到您的应用程序,Android将创建新的进程为你的应用程序,然后它会重新仅是在活动的栈顶(即活动:对,这就是一个展示)。在这种情况下,新创建的活动将无法通过accesing获得对IObject Globals.myObject ,因为这个过程已经被新建的,且该成员变量为null。

Android can kill your process if your application is in the background pretty much any time it wants to. When the user then returns to your application, Android will create a new process for your application and then it will recreate only the activity that was on the top of the activity stack (ie: the one that was showing). In that case, the newly created activity will not be able to get the iobject by accesing Globals.myObject because the process has been newly created and that member variable is null.

要解决这个问题,您可以:

To get around this you can either:

  1. 在确定通过检查你的进程已经死亡,重新启动( Globals.myObject == NULL 并做出相应的反应 - 告诉他需要回去,或者只是去用户回你自己,或显示一个对话框或其他)
  2. 保存对象时,Android把的onSaveInstanceState()在你的活动(其中Android将发送您的应用程序在后台之前做的)和恢复对象的的onCreate()
  1. Determine that your process has been killed and restarted (by checking Globals.myObject == null and react accordingly - Tell the user he needs to go back, or just go back yourself, or show a dialog or whatever)
  2. Save the object when Android calls onSaveInstanceState() in your activity (which Android will do before sending your app to the background) and restore the object in onCreate()

希望这两个回答你的问题,并介绍了该怎么办了。

Hopefully this both answers your question and explains what to do about it.

编辑:添加关于为什么意图包含序列化(Parcelable)的对象,而不是对象本身的更多信息

  1. 当你调用 startActivity() startService() Android的最终可能会开始活动或服务于另一个进程。在这种情况下,如果你的意图传递了一个对象,机器人会以某种方式需要序列化对象,将它传递给其他进程。因为隐式意图决议即Android使用,以确定哪些组件获得处理意图的,呼叫者可能或可能不知道哪些组件将开始使用。

  1. When you call startActivity() or startService() Android may end up starting the activity or service in another process. In this case, if you passed an object in the Intent, Android would somehow need to serialize that object to pass it to the other process. Because of the "implicit Intent resolution" that Android uses to determine which component gets to handle the Intent, the caller may or may not know which component will get started.

Android的节省意图的内容,因为各种原因:

Android saves the contents of Intents for various reasons:

一个。机器人可以在任何时候终止进程。如果这样做了,用户希望返回到应用程序,机器人创建一个新的过程,然后根据需要重新创建在这一过程中的活动。要创建活动的Andr​​oid也需要提供给活动的意图。如果进程已被杀死,然后在任何意图物都必须保存和恢复。因为意图包含序列化的对象,这不是一个问题,因为需要重新创建这些。 B. PendingIntents是使用的Andr​​oid作为一个方式为操作系统,作为一个意图的发送者的代理。一个Android组件可以创建一个PendingIntent,并把那个给操作系统,以便它可以在一段时间后自动发送这一意图。发送组件可以是或不是处于活动状态的PendingIntent实际发送的时间。这意味着,可以在一个PendingIntent传递任何对象必须能够被序列化,这样机器人可以抓住它,即使调用组件不再存在。

A. Android can kill a process at any time. If it does that and the user wants to return to the application, Android creates a new process and then recreates the activities in that process as needed. To create the activities Android also needs to make the Intents available to the activities. If the process has been killed then any "objects" in the Intents would have to be saved and restored. Because the Intents contain serialized objects, it isn't a problem to recreate these as needed. B. PendingIntents are use by Android as a way for the Operating System to act as a proxy for the sender of an Intent. An Android component can create a PendingIntent and give that to the Operating System so that it can trigger the sending of that Intent at some later time. The sending component may or may not be active at the time that the PendingIntent is actually sent. This means that any object that could be passed in a PendingIntent must be able to be serialized so that Android can hold on to it even if the calling component no longer exists.

意图不是要作为组件之间的一般的参数传递机制。当然,你可以用它这样,但你也可以使用其他的(容易)的机制。在给定的过程中,你可以通过周围的物体使用标准的Java机制。没有什么错用静态(类)变量这一点。

Intents are not intended as a general "parameter passing" mechanism between components. Of course you can use it like that, but you can also use other (easier) mechanisms. Within a given process you can pass objects around using standard Java mechanisms. There is nothing wrong with using static (class) variables for this.

这篇关于意图和Parcelable对象的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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