在 Android Activity 之间高效传递自定义对象数据 [Mono Android] [英] Efficiently Passing Custom Object Data between Android Activities [Mono Android]

查看:27
本文介绍了在 Android Activity 之间高效传递自定义对象数据 [Mono Android]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被这个问题难住了一段时间.我正在开发一个 android 应用程序,该应用程序可以存储一个人的渔获量、最喜欢的钓鱼地点、钓具箱库存和其他数据.我所有的类都是可序列化的,并且可以在迄今为止似乎有效的活动之间保存和加载.但我预测,随着存储的数据越来越多,应用程序将开始运行缓慢.

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;}
}

然后在您的活动中:

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

所以我以前从来没有真正听说过这种方法,我不确定这会贯穿整个应用程序过程(我觉得无论哪种方式都必须通过序列化加载数据.这就是我想要的应用待办事项:

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. 如果找到设置文件,使用序列化加载之前的 FishingData 对象(我知道怎么做)
  2. 如果没有,则创建一个新的干净的 FishingData 对象以供稍后保存(我也知道这一点)
  3. 既然我们有了 FishingData 对象,我如何确保不必在每个活动中重复步骤 1-2.我怎样才能以某种方式将 FishingData 对象传递给下一个活动,并确保它在应用程序仍然存在时全局存在.我只想加载一次(通过序列化)(<--不知道如何执行此操作)并且仅在用户向此对象添加数据时才保存它(我知道如何执行此操作)做).
  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).

任何帮助将不胜感激.这让我很烦恼,我似乎无法弄清楚这一点.这似乎是一件很常见的事情,但我没有找到任何详细信息.

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.

推荐答案

您可以使用这种方法,只要您的 Application 对象还活着,它就会一直存在(这意味着它 贯穿您的整个应用程序和活动).您可以阅读有关使用存储在 Application 对象中的全局变量的更多信息 这里.我不认为单声道会产生影响,这会阻止您使用这种方法.

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.

这篇关于在 Android Activity 之间高效传递自定义对象数据 [Mono Android]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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