I have a couple of applications that display downloaded content.
So, data is get in XML via REST, and then parsed to ArrayList<MY_MODEL_CLASS>.
Applications consist of ViewPagers with Fragments, they also have nested Fragments. Data is loaded when user opens corresponding fragments.
Currently all ArrayLists are usual class fields inside Fragments(private ArrayList<Event> downloadedEvents;). To prevent data loss and re-downloading after orientation change, fragments are set to setRetainInstance(true);. But child fragments doesn't allow to do that, so data is getting lost and re-downloaded.
Of course, I can save instance state, save ArrayList to Bundle as Parcelable, make ArrayList static, etc.
My question is more theoretic, that practic: I would like to know good practices of holding downloaded data inside application while it's running without writing it to persistent memory as SQLite.
My variants are:
Store data in fragments, which display corresponding data, and save instance state to preserve ArrayList between orientation change.
Store data in fragments, but just make fields static to preserve between object re-creation.
Make one singleton object that stores all data for all fragments.
But I suppose, that none of them is really good.
What solutions (patterns) do professionals use?
Thanks! :)
Related
My app contains about 8 activities having different Listview's. As the data shown in each activity is constant (cannot be changed by user), what method should I use to save the listview items ?
Should I make a arraylist, sqlite db, or other method.
As the list may be long I want a easy structured method to add data on my PC then shown it on my app.
*Adding data is updating my app with latest list
If you want to add data later on and have it updated on all user phones, you should make an API with a database behind it, where you can send an HTTP request to retrieve data for each list separately.
This way, you can change list content however you want and it will be the same on all devices and you don't have to store it on the phone (maybe only cache it to reduce load on the API). The only bad side is that you need a server and a domain.
May I suggest some simple backends: Flask(Python) or NodeJS (Javascript).
I need to store an array of Custom Objects to populate a ListView in android. I fetch the data to populate the ListView in a Fragment that is a part of NavigationDrawer (The Fragments are replaced frequently). The next time I start the Fragment, data is fetched again. I want this data to be persistent even when I go to another Activity and come back.
I'm not considering using a database as I don't need the data to be restored when the user restarts the app. I just need the data as long as the user is actively using the app.
I've considered the following methods:
Singleton class
Subclassing the Application class
Saving the Instance state in the Fragment/Activity
Is it fine to store an array of 2000 objects in the Application subclass? Is there a better methos to do this?
You shouldn't consider using the Singleton Class, if you want your data to be persistent.The best Android practice to store persistent data are :
1.Application Preferences
2.Files
3.contentProviders
4.SQLite DB
You can read more about it here : http://developer.android.com/guide/faq/framework.html
I need to be able to store a list of many bitmaps with multiple values for each, for instance a string name, a int resource ID and a boolean flag. Because of this I am leaning towards using SQLite to store this data and retrieve what I need into a list on demand. Currently I am handling these bitmap lists by syncing multiple ArrayLists with the values so the nameList, resourceIDList and flagList indexes are synced across. This works but the class I created to create these lists have several hundred items per list and when I need to add a new entry it can be a real pain as I have to update each list. This method also feels sloppy so if anyone can chime in on a better solution I am all ears!
Why don't you use android sqlite to store and retrieve your data. Then you can conveniently display the retrieved data on the list. Below I have provided some useful links.
http://www.vogella.com/tutorials/AndroidSQLite/article.html
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
I'm making a game of Tetris on android as a project for school and right now im using shared preferences in order to save the current state of the game so that it can be resumed on a later time , i've come to realize that when you store over 100 or so preferences the sharedprefernces object starts working in a strange way , i can save everything but when i try to call the editor to clear (e.clear + e.commit) it wont remove the preferences.
i would appreciate any help regarding this issue
thanks
SharedPreferences are good and lightweight mechanism how to persist data.
But i think for game it's not a win at all. SharedPreferences are usually used for persisting non-structured data for example if you have some application that requires login and when User is logged in successfully you can save this state to SharedPreferences and in next Activities just check it whether User is logged in or not. But in the game you have (i guess for sure) structured data-structures (for instance players and their properties (values) like reached score, loses, wins etc.).
So i suggest you to think about another mechanism for data persisting. Specifically try to think about a possibility to use classic object serializing or and usage of SQLiteDatabase which provide more complex solution how to persist structured data.
A main advantage is that you can persist (serialize) whole objects and then simply deserialize them (and not persist them as specific "chunks" in SharedPreferences). Regarding to SQLite, it provides almost same solution as classic serializing but objects are represented as tables in database.
If you need to remove specific values use this:
SharedPreferences.Editor.remove() followed by commit()
To remove them all SharedPreferences.Editor.clear() followed by a commit()
(references here https://stackoverflow.com/a/3687333/1584654).
However If the values remain limitated, for Shared Preferences should not be an issue.
What is the best way to maintain a static cache on an Android device?
For example, say I have the look-up tables Type and Category - these two tables will rarely change....but if they do, they must be refreshed.
How can I store this data in some sort of cache on an Android device, while still updating them when something changes on the server? Seeing as they rarely change, I would like to minimize the amount of data being sent/received from the device.
Note - there are more look-up tables than this, I am just using two as an example.
Create A singleton instance of static HashTable/HashMap and store objects which contains data to populate screens ( Like primitive, pojo ,collections ect ) and a flag as well which will notify whether that value is being refreshed . set this value when you read any change from server . update with fresh value .