传递到活动意图引用 [英] Passing references to Activity Intent

查看:163
本文介绍了传递到活动意图引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关相当长一段时间,我有很多麻烦传递变量从一个活动到另一个,和我平时所必须解决的一些pretty丑陋的静态类黑客,使其工作。

For quite some time I've had troubles passing variables from one Activity to another, and I've usually had to resolve to some pretty ugly Static-class-hacks to make it work.

通常沿东西,我打电话与活动的类型,与活动要求的变量沿着一个静态方法的线路。这些被存储在一个静态变量,并检索在上述活动的构造函数。

Generally something along the lines of a static method that I call with the type of the Activity, along with the variables the Activity requires. These gets stored in a static variable, and retrieved in the constructor of said activity.

就像我说的,pretty难看。还有的因为没有这样的事情myActivity.StartActivity(新活动);。所有重载StartActivity的花费要么是意图或typeof运算(MyOtherActivity)。

Like I said, pretty ugly. And there's no such thing as "myActivity.StartActivity(new Activity);". All of the overloads for StartActivity takes either an Intent, or a typeof(MyOtherActivity).

所以我的问题是,有我完全误解活动的概念,还是我只是缺少一个完全显而易见的方法参数传递给他们?

So my question is, have I completely misunderstood the concept of Activities, or am I simply missing a completely obvious way to pass arguments to them?

@Edit:我之所以想通过一个实际的对象引用,而不是简单地将对象的副本,是因为我想从上覆活动传递一个视图模型,下至新的活动。当然这种视图模型所做的任何更改,应在父活动,这只会是可能的,如果两个activy的的ViewModels指向同一个实例体现。

@ The reason I want to pass an actual reference to an object, instead of simply a copy of the object, is because I'm trying to pass a View Model from an overlying Activity, down to the new Activity. And of course any changes made to this view model, should be reflected on the parent activity, which will only be possible if the the two activy's viewmodels points to the same instance.

我正在写使用Xamarin.Android应用程序,但code是C#和Java之间几乎相同,所以无论是在那些语言答案是好的。

推荐答案

的问题是,Android的可以杀死过程中随时托管的应用程序(如果它是在背景中)。当用户然后返回到您的应用程序,Android将创建新的进程来承载您的应用程序,将重新创建活动在堆栈的顶部。为了做到这一点,Android的保持意图,以便它可以重新创建意图的一个连载版它传递到活动。这就是为什么在意图必须 Parcelable 序列化

The problem is that Android can kill the process hosting your app at any time (if it is in the background). When the user then returns to your app, Android will create a new process to host your app and will recreate the Activity at the top of the stack. In order to do this, Android keeps a "serialized" version of the Intent so that it can recreate the Intent to pass it to the Activity. This is why all "extras" in an Intent need to be Parcelable or Serializable.

这也是为什么你不能传递到对象的引用。当Android的再现过程中,没有这些对象将不复存在。

This is also why you cannot pass a reference to an object. When Android recreates the process, none of these objects will exist anymore.

另外需要考虑的一点是,不同的活动可能会在不同的进程中运行。即使从相同的应用程序的活动可以是在不同的过程(如果清单指定这一点)。由于对象引用不跨进程边界的工作,这是另一个原因,你不能在一个引用传递到对象的意图

Another point to consider is that different activities may run in different processes. Even activities from the same application may be in different processes (if the manifest specifies this). Since object references don't work across process boundaries, this is another reason why you cannot pass a reference to an object in an Intent.

这篇关于传递到活动意图引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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