使用Android应用程序类持久化数据 [英] Using the Android Application class to persist data

查看:147
本文介绍了使用Android应用程序类持久化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个相当复杂的Andr​​oid应用程序,需要稍微大量有关应用程序的数据(我会说一共有约500KB - 这是大的移动设备?)。从我可以告诉,任何方向变化的应用程序(在活动中,要更加precise)导致活性的完全破坏和娱乐。根据我的调查结果,应用类不具有相同的生命周期(即是,对所有意图和目的,始终实例化)。是否有意义存储的状态信息的应用类的内部,然后从活动引用它,或者是通常不是由于在移动设备上存储的约束可接受的方法?我真的AP preciate此主题的任何建议。谢谢!

I'm working on a fairly complex Android application that requires a somewhat large amount of data about the application (I'd say a total of about 500KB -- is this large for a mobile device?). From what I can tell, any orientation change in the application (in the activity, to be more precise) causes a complete destruction and recreation of the activity. Based on my findings, the Application class does not have the same life-cycle (i.e. it is, for all intents and purposes, always instantiated). Does it make sense to store the state information inside of the application class and then reference it from the Activity, or is that generally not the "acceptable" method due to memory constraints on mobile devices? I really appreciate any advice on this topic. Thanks!

推荐答案

我不认为500KB将是一个大问题了。

I don't think 500kb will be that big of a deal.

你所描述的是我到底如何解决在活动中的数据丢失了我的问题。我创建了应用程序类的全球独立的,并能够从我使用的活动对其进行访问。

What you described is exactly how I tackled my problem of losing data in an activity. I created a global singleton in the Application class and was able to access it from the activities I used.

您可以在全球各地的单身传递数据,如果它要被使用了很多。

You can pass data around in a Global Singleton if it is going to be used a lot.

public class YourApplication extends Application 
{     
     public SomeDataClass data = new SomeDataClass();
}

然后,通过调用它的任何活动:

Then call it in any activity by:

YourApplication appState = ((YourApplication)this.getApplication());
appState.data.UseAGetterOrSetterHere(); // Do whatever you need to with the data here.

我商量<一个href="https://web.archive.org/web/20130818035631/http://www.bryandenny.com/index.php/2010/05/25/what-i-learned-from-writing-my-first-android-application"相对=nofollow>在这里我的博客文章,在部分全球辛格尔顿。

I discuss it here in my blog post, under the section "Global Singleton."

这篇关于使用Android应用程序类持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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