Using the Android Application class to persist data
来源:互联网 发布:淘宝搜索什么能看片 编辑:程序博客网 时间:2024/04/27 07:42
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!
Answer:
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.
I discuss it here in my blog post, under the section "Global Singleton."
- Using the Android Application class to persist data
- How to: Send Data Using the WebRequest Class
- persist to the end
- A Data Access Layer to persist business objects using attributes and reflection - Part III
- A Data Access Layer to persist business objects using attributes and reflection - Part I
- Using Python to show the test data
- Android-WARNING: Uninstalling will remove the application data! Do you want to uninstall the existi
- Using Application Engine to Import Data from Files
- fit the “model” to the training data using that method
- DELPHI / Determine The Path To The Users Application Data
- 2 - Much ADO about Data Access: Using the Data Access Application Block
- Add data to the Access database using ADO
- Using R to read and plot the csv data
- Using the data layer API to query your Kentico database
- media foundation (Using the Source Reader to Process Media Data)
- How to pass data to a Flex application using SWFObject 2.0
- An application sends the WM_COPYDATA message to pass data to another application
- Using the Debug Class
- Android笔记(3)——系统时间的获取与音量控制
- hdu 3666 THE MATRIX PROBLEM //2010哈尔滨现场赛
- DataGridView的一般用法(1)
- Windows 电脑命令一览
- MSSQL设计主键类型
- Using the Android Application class to persist data
- 在Oracle中使用rank()over()排名的问题 .
- 嵌入式笔试题荟萃
- Build the Android work environment in Ubuntu Linux
- vs2005取消代码编辑区自动换行
- Android 中自定义View的应用
- Git Submodule
- Struts1.x系列教程(10):Validator验证框架入门
- 打造持久提权重的网站