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
Related
I have a dropdown list in a fragment that I access by referencing a ViewModel I initialize in the main activity, said ViewModel retrieving data from a mySQL database using Volley. This normally works as almost always the data has been successfully retrieved by the time the user opens the fragment requiring it, however, if I update the database and refresh my ViewModel's contents this is almost never the case (upon update I immediately return to the fragment where I need the data)
what I'd like is to be able to show a loading prompt until confirmation of successful retrieval of the data, but all resources I can find on this use the now deprecated AsyncTask, so I'm not sure how best to approach this.
I need to store a large amount of data. Let’s say I need to store the list of users and their profile picture when the app going to rotate my app is crashed. Is there any trick how I can store the data?
You can save your data as a file or in Room and read if afterwards. ViewModel is not created to store huge sets of data.
https://developer.android.com/training/data-storage/files
If it is a simple data you can use onSaveInstanceState but since you have a large data, you can store your data using Room and integrate ViewModel class to handle configuration change. Can you post your code?
https://developer.android.com/topic/libraries/architecture/viewmodel
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 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! :)
I have a program with a main activity which spawns 4 different intents with lists in each. There is a database used to track all of the choices from these lists. Currently with the way that startActivityForResult() works I am having the program send back a comma delimited string which is saved in an array in the main activity and upon main 'submit' it is all saved to the database. The lists represent choices made on a per-day basis but each list corresponds to the same day. I am using putExtra() to send the correct list to the activity.
This can be an issue if the user closes the program or android kills the app the array may be lost. I want to update the current dates data from each spawned intent but when I would do this it would create a new entry.
Main question: Can I set a global date value (that the user sets in main) that each intent can reference to use update for my sql statements. I want to make sure that there is no loss in data. Currently writing to the database is only done in the main activity upon submit but I would like to have the app write to the db from each intent.
This question is very abstract in my brain and if there is a need to clarify I will do my best. (I am at school and just brain storming things to try when I get home)
Write the array to Shared Preferences in onPause() and onTerminate(). You can use the array element number as the key and whatever is in that array element as the value.
I can't quite work out where you're going with the global date/time stamp, but if the point is to prevent data loss, Shared Prefs can be damned handy.