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()
Related
I'm working on an android cooking app (using java) and the homepage has a recycler view populated with recipes which users can like (similar to FaceBook posts). The like button is a checkbox, what is the best way to save the state of the like checkbox for every recipe, so when the user signs out of the app and sign in again they will not like the same recipe more than one time.
Is using SharedPreference a good idea in this situation?
**im using MySql as a database and firebase is not used.
You could definitely do it using SharedPreferences - but I believe this is more ideally designed for 'preferences' of an application rather than 'application' behaviour state.
You might want to have a look at : https://developer.android.com/topic/libraries/architecture/saving-states?authuser=1
Here you will see some options of how to serialize and persist UI states to memory. I have not actually used something like this before - in the past only using SharedPreferences.
You could of course also create your own storage method, or solution but why bother when ones already exist.
Since you are already using a MySQL database, your application is already grabbing the rows from the DB to show them, so why not add another column called 'checked' which is a boolean type. Then when you get your recipes you get the 'checked' variable, and if it is true, then set your UI state of the checkbox to checked, otherwise false :)
The benefit of this approach is that if your app became 'hybrid' and you wanted a website for it too - the data on the database becomes centralised, meaning your persisted user state is the same on mobile as it is web experience, which is a nice benefit!
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.
I am making a contacts app that presents the user with the following Buttons, "Create New Contact", "Edit Contact", "Delete Contact", "Display Contact" and "Finish".
There is a Contacts List activity which must be entered/displayed when clicking on Edit, Delete, or Display. When creating a contact in the "Create New Contact" Activity, I would like to send the Contact object (it implements Parcelable) to an ArrayList within the Contacts List Activity WITHOUT starting it, once the Contact is added, I would like to return to MainActivity.
I am unsure how to go about this as using Intents results in going straight to the activity and I am also unsure how to cause the ArrayList within the Contacts List Activity to save.
Any suggestions? I do not need exact code but I am unsure what direction to go in to accomplish this task.
In my opinion the data model should live in the context of the app and not in the context of an activity. Then the data is accessible from all activities. Now the only object you need to pass by intent is the id of a data set.
This general principle also enables what you want to do, start a new activity, do some data manipulation and send a new intend to the next activity without displaying the previous one at all.
There is a logical problem: if there is not a Activity instance, why do you need to send data to it(even not existed)?
Alternatively, if you'd like the data existed within the whole user lifecycle, it needs to save edited data(ADD,EDIT,REMOVE) in somewhere like local storage or Database. And fetch data(then parse it to specific object) from persistent layer when start the presenter activity (where received data and presented them to user).
My app is mainly a timer that counts down from 20 hours.
I am wondering which method I should use for saving the information necessary for resuming the app with the timer showing the correct value.
I don't know if I should use savedInstanceState or SharedPreferences?
My plan of action is to:
1. create a TimeStamp on the "Timer Start" button
2. save the TimeStamp variable with either the savedInstanceState or SharedPreferences
3. When app is closed and re-opened, get the TimeStamp variable
4. change progressBar status to display the correct remaining time based on the TimeStamp instead of restarting the progressBar from 0.
You should go with shared preferences. Shared preferences are designed to hold persistent state while saved instance state is mainly designed for restarting an activity after configuration changes.
See Google Developer Activity JavaDoc page
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.