I want to persist some edit text data after application is terminated completely. I have two activities MainActivity1.java and MainActivity2.java.
If user enters data partially and closed the application when he comes back application need to be pick up all the data which he was entered in previous stage.
Scenario:
Hence I am using ListView I have map of editText values in the form of key value pair. Currently I am working on MainActivity2.java file which has list view. So I am saving the map data in to Internal Storage of device when MainActivity2 is on onPause() state. So on OnResume() it is showing all data into edit text.
Now the problem is if I terminate the application completely then it is not picking up the data which I have stored in my Internal Storage.
So if I need to store data when application is closed completely which method I need to call.
Note : I am using file read and write to store and get data from internal storage
Whenever you write any data into the Edittext, you may save it to your Shared Preference (if the data value is not much) else you can create a Database using SQLite or Room (if the set of data is higher).
In both the cases, the data will persist even if you close the application, make sure to save the data after you have input the data in the edit text.
https://developer.android.com/training/data-storage/shared-preferences -Shared Preference
https://developer.android.com/training/data-storage/sqlite - SQLite
Look SharedPreferences or SQLite. Once you do you will basically be saving your values in SharedPreferences or SQLite in an onPause() / onDestroy() method, and then in your onResume() / onCreate() methods you will retrieve the previously stored data.
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.
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).
So what I am trying to do is save the current progress made by the user in Textviews.java through sharedPreferences. E.g. the activity contains several textviews and the user types in them. When the user presses back, it should save the current state of what is typed in the textviews.
And once the user presses the exit button on Main.java, an alert dialog comes up asking the user if he would like to save what he typed. If he selects yes, then it saves and the app closes, if not then it doesn't save and the app closes.
So far I have managed to save what the user does when he presses back when inside Textviews.java but I have several questions regarding how to commit the save when the alert dialog comes up when the user press 'yes' on the alert dialog. And also, loading the data when the user goes back to the Textviews activity.
I have a couple of questions regarding sharedPreferences.
1) Throughout the WHOLE app, can it only have one sharedPreference() object to store their data in? Or can there be multiple objects. If multiple objects is allowed, then does 1 activity essentially get one sharePreference object or can one activity have several sahrepreference objects to store their data?
2) How can I access other activities shared preference datas?
3) How can I know if a sharedpreference file exists?
1) Throughout the WHOLE app, can it only have one sharedPreference() object to store their data in? Or can there be multiple objects. If multiple objects is allowed, then does 1 activity essentially get one sharePreference object or can one activity have several sahrepreference objects to store their data?
There can be multiple Shared Preferences. When you try to get Instance of SharedPreference, you will give it a name.
getSharedPreferences(SHARED_PREFS_FILE_NAME, Context.MODE_PRIVATE);
SHARED_PREFS_FILE_NAME is name of shared preference. if you give different name at different place you will create multiple Shared Preference.
SharedPreference is XML file, different file name, different SP.
2) How can I access other activities shared preference datas? 3) How can I know if a sharedpreference file exists?
When you call this method >> getSharedPreferences(SHARED_PREFS_FILE_NAME, Context.MODE_PRIVATE);
it checks if the sharedPreference file exist, if yes, it will open the existing file, or will create new file.
You can save data from anywhere in the app and can get it back from anywhere, make sure you pass same SharedPreferences name in getSharedPreferences
Here is what Android Document says about [getSharedPreferences][1]
public abstract SharedPreferences getSharedPreferences (String name, int mode)
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
Edit :- To remove Shared Preferences specific values or complete file clear, follow below Reference answer
To remove specific values: SharedPreferences.Editor.remove() followed by a commit()
To remove them all SharedPreferences.Editor.clear() followed by a commit()
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 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.