As I know, when a user does "clear data" or "disable" any application, it's shared preferences are cleared and database is also cleared.
So my question is,
Is there any case, that user can perform, in which shared preferences gets cleared but database remains intact ?
Clear data means user wants to clean complete data related to app (sharedpref, cache, db). If you want some data to remain intact even after user tap on "clear data", keep in device storage.
Clear Data will clear everything, Shared Preference, Cache, DB.
If you really want to clear only shared preference (some/all) you can do it programmatically by iterating over Shared Preferences like this:
Map<String, ?> allEntries = prefA.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Log.i("debug", entry.getKey() + ": " + entry.getValue().toString());
//put delete/edit logic here for some shared preference
}
This logic can be placed based on your app version, or some other logic like when user logs out, but this action would be performed in your app only.
I dont think Android Applications settings allows any option where you can clear only shared preferences and not local db.
You can't save an ArrayList of Objects in Shared Preferences.
From Android Documentation: https://developer.android.com/guide/topics/data/data-storage.html#pref
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive
data types. You can use SharedPreferences to save any primitive
data: booleans, floats, ints, longs, and strings. This data will
persist across user sessions (even if your application is killed).
The easiest solution would be to implement serializable in your objects and save them in internal storage.
https://developer.android.com/guide/topics/data/data-storage.html#filesInternal
There is an option to put a database in apk directly. Of course if you use already completed database in your case
Related
Suppose i made an app which uses shared preferences,
will the data stored with shared preferences be deleted when user clears app storage?
if yes then what is alternative to store/retrieve important data faster which will not get erased by user?
i tried firebase-database, but it is lagging the speed/performance i require.
will the data stored with shared preferences be deleted when user clears app storage?
Yes.
what is alternative to store/retrieve important data faster which will not get erased by user?
Store it on a server.
i tried firebase-database, but it is lagging the speed/performance i require.
Then store it on a faster server, I guess. Or, use a local file as a cache, with the server acting as a backup location, should you need to restore the data at a later point.
Can I use a column in table to check if user has logged in.
When user logs in I set value to 1. When he logs out I set to 0.I dont want to use SharedPreferences. Is using it like this inefficient.
I used sharedpreferences first. I set username in the sharedpreference along with other preferences and display username in the nav drawer. When I install the app in another device, I think even the sharedpreference file will be installed and I saw that in the new device the username is displayed in the from the sharedpreference file though that user doesn't exist in the table in that device's database
When user logs in I set value to 1. When he logs out I set to 0
Here, what will happen is when you set values to 0 or 1 you have to open and close database everytime and will be a hastle to maintain. you have to check it each time what flag is. while in SharedPreferences it will be globally accesible and easy to set flag.
I would suggest you to use SharedPreferences because
SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through
Check SharedPreferences and SQLite.
Better to use Shared Preferences instead of using Database for such a small thing.
Refer this : http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
I need to use few variables frequently but I want know that which one will give fast response SQLite Database or Shared Preferences ?
Shared Preferences:
What it is?
Shared Preferences is an API from Android SDK to store and retrieve application preferences. Shared Preferences are simply sets of data values that stored persistently. Persistently which mean data you stored in the Shared Preferences are still exist even if you stop the application or turn off the device. SharedPreferences available at the Activity level or shared across all Activity in application package.
When to use Shared Preferences?
o You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
o Think of a situation where you want to save a small value (Login Credential) that would be referred later sometime when user launches the application. Then shared preference comes into action
How to Use?
SharedPreferences sh_Pref = getSharedPreferences("Login Credentials", MODE_PRIVATE);
Editor toEdit = sh_Pref.edit();
toEdit.putString("Username", "Name");
toEdit.putString("Password", " Password");
toEdit.commit();
SQLite Database:
Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS.
How to use ?
To use SQLite in Android, a java class should be created as a sub class of SQLiteOpenHelper. This class will act as a database controller which will have the methods to perform the CRUD operations. This custom java class should override the methods named onCreate() and onUpgrade().
onCreate() method will be called for the first time when the Android application is run. First the database instance should be created using the method like getReadableDatabase() or getWritableDatabase() based on the type of access required. Android supports this method by providing in-built methods. For that, SQLiteDatabase class should be used.
I know this topic has been discussed before on Stack Overflow. But there are still some things that are not clear when I read previous posts about it. So here they are:
I know that we use shared preference for small datasets and sqlite for large data manipulation, so if we just want to save a username and password should we use shared preferences?
Won't shared preferences be lost when user uninstalls the app? For example I download an app called abc and save my username and password. Then I uninstall this app from one phone and try to access it from other phone using the same username and password. Will this be saved using shared preferences or the data be lost?
What are the main reason we use one over the other beside large and small datasets?
You can think of the difference between shared preferences and an SQLite database in terms of data size but that isn't entirely accurate. A better way to think of it is in terms of the structure of the data you want to store.
Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.
Both data sources are local but something you should be aware of is the ability to backup your application data to cloud storage that is linked to the user's Google account. This makes it much easier for your users to change devices and for their applications to easily transfer to the new device. For more info take a look here.
In the situation you described about you will lose the user name and password in both situations. The data is stored on the phone, when you uninstall the application, the data that some with it will also be lost. The user will have to re-enter this information.
You can save the user name and pass in either the shared Preferences or a DB, that is personal preference. Just make sure you lock either down, i.e. don't share the DB or Shared Preferences that you keep this information in.
As for the difference... shared Preferences should hold well... shared Preferences... here is an example:
If I create an option to change the background color, I will store all available options in a DB that can be loaded into a adapter view for the user to choose from. But I will store the color that they have selected in the Shared Preferences. This way when the application load I can get the Shared Preference value of the background color that should be used.
SharedPreferences is used for just that, storing user preferences shared application-wide. You can use it, for example, to store a user's username, or perhaps some options he or she has configured in your app in which you want to remember.
SQLite is a relational database. It's used to store your application's data, not preferences or configuration information.
Both are stored locally on the device.
1.SharedPreferences stores only Boolean, int, float, long, String five kinds of simple data types, such as can not be conditional query. So, whether SharedPreferences data storage operation is how simple it can only be a supplement of storage, but can not completely replace other data such as the SQLite database is stored.
2.SharedPreferences based on the XML file to store key-value key used to store configuration information(mainly user preference for your application).
3.Sharedprefrece just like cookies in web which store some basic information at client side.
both store their data locally, so uninstalling the app will delete both. other than that, SharedPreferences is easier to program, and you're right about the data amounts.
In general, shared preferences should be used if you want to allow your user to directly manipulate certain data fields. Shared preferences are basically user preferences; if you would like the user to reconfigure the app to behave in different ways, you should expose that functionality as a shared preference. On the other hand, the SQLite database should be used if you want to limit the visibility of the data to just the application, if you want a stronger guarantee that the data be persistent, and if you want the application to behave independently of what is stored in the database. Of course, you can use both in one application.
Shared preferences and the database are part of local data that the application stores. If you uninstall the application, both of the data stores will be removed.
i have made application in which Homesceen stored username and password than redirect on second page .when i close this application or power off device than again click on application icon than i want to redirect on second page . so what can i do for doing this ? can we get power off functionality or anything else ? how to stored variable in memory of this application ?
You could used Shared Preferences to store your login and password :
The SharedPreferences class provides
a general framework that allows you to
save and retrieve persistent key-value
pairs of primitive data types. You
can use SharedPreferences to save
any primitive data: booleans, floats,
ints, longs, and strings. This
data will persist across user sessions
(even if your application is killed).