有效地将自定义的Andr​​oid活动的对象数据[单声道的Andr​​oid] [英] Efficiently Passing Custom Object Data between Android Activities [Mono Android]

查看:182
本文介绍了有效地将自定义的Andr​​oid活动的对象数据[单声道的Andr​​oid]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经难倒与此有一段时间了。 我工作的一个Android应用程序存储一个人的渔获量,喜爱钓鱼的地点,钓具箱库存等数据。我所有的类都是可序列化,并且可以保存和活动的加载,这似乎迄今的工作。但我predicting随着越来越多的数据存储,应用程序将开始运行缓慢。

I've been stumped with this for a while now. I'm working on an android app that stores a person's fish catches, favorite fishing locations, tackle box inventory and other data. All my classes are Serializable and can saved and loaded between activities which seems to work thus far. But I'm predicting as more and more data is stored, the app will start running slow.

什么我基本上问的是有没有什么办法留住整个应用程序这个数据,所以我没有加载它的每一个新的屏幕上弹出的时间。我已经找到了以下信息,以帮助,但它需要一个更加清楚一点,我明白了:

What I'm basically asking is there any way to retain this data throughout the entire application, so I don't have to load it every time a new screen pops up. I've already found the following information to help but it needs to be a little more clear for me to understand:

另外一个论坛上说,你可以在应用程序对象东东吧:

Another forum said you could stuff it in the Application object:

[Application]
public class MyApp : Android.App.Application {
    public MyApp(IntPtr handle)
        : base (handle)
    {
    }

    public FishingData Data {get; set;}
}

然后在你的活动:

Then within your Activity:

((MyApp) this.ApplicationContext).Data = value;

所以,我从来没有真正听过这样的方法之前,我不知道这会度过整个应用程序(我觉得无论哪种方式,将不得不通过序列化加载数据。这就是我想要的该应用程序TODO:

So I've never really heard of doing this approach before and I'm not sure this will live through the entire application process (I feel like either way it's going to have to load the data via serialization. Here's what I want the app todo:

第一个活动是主菜单,下面必须做的时候,屏幕载荷:

The first activity is the main menu and the following must be done when the screen loads:

  1. 如果找到一个设置文件,使用序列加载previous FishingData对象(我知道如何做到这一点)
  2. 如果没有,然后创建一个新的清洁FishingData对象后保存(我知道这也)
  3. 所以,现在,我们有一个FishingData对象,我怎么保证我没有重复步骤1-2的每一个活动。我怎么能以某种方式FishingData对象传递到下一个活动,并确保其生活globaly,而应用程序还活着。 我只是想(通过序列化),一旦加载它(小于 - 不知道如何做到这一点),并保存它,只有当用户将数据添加到该对象(我知道如何做的)。
  1. If a settings file is found, use serialization to load a previous FishingData object (I know how to do this)
  2. If not, then create a new clean FishingData object to save later (I know this as well)
  3. So now that we have a FishingData object, how do I ensure that I don't have to repeat steps 1-2 in every activity. How can I somehow pass the FishingData object to the next activity and ensure that it lives globaly while the app is still living. I only want to load it once (via serializing) (<--Don't know how to do this) and save it only when a user adds data to this object (which I know how to do).

任何帮助将AP preciated。这是窃听我,我似乎无法想出解决办法。这似乎是这将是一个普通的事情,但是我还没有得到任何运气找到任何详细信息。

Any help will be appreciated. This is bugging me I cant seem to figure this out. This seems like it would be a common thing to do but I haven't had any luck finding any detailed information.

推荐答案

您可以使用此方法,它会活,只要你的应用程序对象是活的(这意味着它的活在你的整个应用程序和活动)。你可以阅读更多有关使用存储在应用程序的全局变量对象的此处。我不认为单会有所作为,这将prevent您使用这种方法。

You can use this approach, it will live as long as your Application object is alive (Which means it will live through your entire application and activities). You can read more about using global variables stored in the Application object here. I don't think mono would make a difference which will prevent you from using this approach.

这篇关于有效地将自定义的Andr​​oid活动的对象数据[单声道的Andr​​oid]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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