How to save instance of an activity using Jetpack library - java

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

Related

Saving the values temporarily

I have a survey report app, where the user will fill in the data (text and images) but in case he wrote half the report while doing the survey and closed the app to continue later all the data is lost, I am kind of new to programming, I tried to search for a way to do it and found the SQLite but I am not sure of the answer because I don't want to save the data in the app as the report will be sent by e-mail, so as soon as the data is sent I want all the values to be deleted from the app
Please take a look at this link Data and file storage overview.
You can also use the interface SharedPreferences (Save key-value data).
If you want to save your data in an online Database, take a look at Firebase, especially Firebase Realtime Database.
Hope you have all the info.

Best method to show fixed Listview Data - Arraylist / sqlite / any other

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).

Storing temporary downloaded data in Android application

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! :)

Storing data across Fragment and Activity life cycles in android

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

Android arraylist large number of string objects

I am developing a Android application for a website. It has large number of users around 100000. I have to fetch these users to an Arraylist for a custom user search. Is that a good practice to store this much amount of data in an Arraylist (particularly in Android). If not I am planning to use a Sqlite database any suggestions?
You do not want to use a list of any type.
Databases are optimized to store and search through large amounts of data, if you store these usernames in an ArrayList, you would be responsible for ensuring that you efficiently search.
This seems like a poor idea in the first place. Why would you want to have a local copy of all 1lakh+ usernames? This is terrible waste of bandwidth! It would be better if the application could query the database for the usernames it is searching for. You could then store the results only on the client.
ex: SELECT * FROM `user` WHERE `name` LIKE "david"
Store only the results from the query. Not every user.
Make Data Classes and make it Serializable and use file storage.. Because if your using DataBase getting and putting data is a different task... storing file Object is better for data handling..
It seems that it is not a good idea to store the content in an ArrayList. Depending upon the data or your application, you may get a 'OutOfMemory' error. Try persisting the information to a SQLite database or file.
On the other hand, I do not find the necessity to bulk download the 1 lakh user data and store it locally for search on device. You could make your service to do the search and return only the search results. If this is possible, then storing it in ArrayList is not bad. If the size of your arraylist exceeds the amount tolerated by the DVM, you could override onLowMemory callback and empty the list contents. By this way you could prevent your app from being killed

Categories

Resources