Prevent deletion of database or shared preference on clear app - android

I want to prevent the deletion of shared preferences or database on clear such that it will clear only when the app is uninstalled. How can I keep shared preference safe on app. clear?

It is worth noting that there are three types of data clearing in Android, of which your application has no control over:
Clear Data
Clear Cache
Clear Defaults
Clear Data
Clear data will clear everything under the application's working directory. This includes any shared preferences, databases and files saved by the application which reside in the application directory. This will also clear the application's cache, as it is also part of the application's working directory.
Clear Cache
The application has access to a cache directory to save files to, mainly for the purpose of caching such as generated images or temporary files.
Clear Defaults
This options will clear any intents registered to this application.
Uninstalling an application will clear all three sets of data. Therefore the only way to maintain data between installs is to save it to external storage, but note that both the user and every application has access to the external storage and therefore may be modified or deleted at any point.
Links worth reading:
http://developer.android.com/guide/topics/data/data-storage.html#
Android: Save file permanently (even after clear data / uninstall)
What's the difference between clear cache & clear data in Android settings

Related

Can a user delete data stored in shared preferences?

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.

Save information in local Storage

I'm using realm(also tried sharepreference) to save some information.But When I cleared app's data,All information has removed.
I have one question.How I can save some information to my device,witch always 'll be in storage? (if i cleared data or uninstalled my app)
Yes, clearing local data delete shared preferences and data in the local SQL Lite database as well. However you can make a folder for your app in local SD card or in phone's internal storage. These folders will not be deleted even if your app uninstalled. In these folders you can store your data in some sort of a format, it maybe in plain text or it maybe some encrypted files. It's your call.. hope this helps

How to keep shared preference data after clearing the cache of application from settings

I have observed that when i click on clear cache in settings>apps>particular app,
Shared preference data get deleted.
how to keep shared preference data even if i clear the clear the cache? is it possible? if possible means give idea about that
It is worth noting that there are three types of data clearing in Android, of which your application has no control over:
Clear Data
Clear Cache
Clear Defaults
The only way to have persistent data is to use the SD card, but again, users won't like to have the data on their card after the app is uninstalled or users can un-mount the SD card.
Or you can consider:
Storing the data on a remote server with some kind of authentication to retrieve it
Using Data Backup service
Shared preferences is generally used to store temporary information on a user's device. So generally it holds temporary information/data.
To store data that will survive the 'Clear Cache' action, you can store information in an sqllite database.
To do this, you need to implement a content provider that will encapsulate access to the sqllite database, it will help you store and retrieve data that will not be deleted when the cache is cleared. find more information on how to create a content provider here : http://developer.android.com/training/basics/data-storage/databases.html

Android - Life time of different storage options

Android provides following options for data storage:
Shared Preferences - Store private primitive data in key-value pairs.
Internal Storage - Store private data on the device memory.
External Storage - Store public data on the shared external storage.
SQLite Databases - Store structured data in a private database.
Cache - Cache data during application is running.(May be clear on shortage of space)
What I was wondering is the life time of these storage.
Which storage(s) clears on application close (Obviously cache)
Which storage(s) clears on application re-install
Which storage(s) clears on application update
Which storage(s) clears on application un-install
Which storage(s) clears on application clear data
Which storage(s) clears on mobile factory reset
Which storage(s) clears on Rom upgrade/change
An extension to this question is, using which storage my data is more secure in terms of loss and in terms of access by unauthorized resources (users, applications in case of root).
Which storage(s) clears on application close (Obviously cache)
Cache
Which storage(s) clears on application re-install
Cache
Which storage(s) clears on application update
Cache
Which storage(s) clears on application un-install
Cache, SQLite, Shared Preferences
Which storage(s) clears on application clear data
Cache, SQLite, Shared Preferences
Which storage(s) clears on mobile factory reset
It clears all, but not External Storage data
An extension to this question is, using which storage my data is more secure in terms of loss and in terms of access by unauthorized resources (users, applications in case of root).
It is best to store the data in the SQLite in the encrypted form. If your device is rooted then it means you can access even the SQLite.
Application Close : Cache files wont be deleted after application close.
As per android docs,
When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
Application re-install and un-install : Cache, Shared Preferences, Internal Storage and Databases will be removed when un-installing the app.
Application Update: Usually everything you had on previous version will be restored. Docs are also not clear about this.
Application Clear data : Everything except External Storage will be deleted permanently.
Factory Reset: Doing a Factory Reset will erase all the apps and its data except preinstalled ones. You can restore your apps with google account but not data (If data not backed up with BackUp Api.
And for your final question,
There is NO Secure data storage if you store data in device. Even External data storage can be removed with USB file options. For secure data, you should maintain user data on your server and get it on demand.
Hope I was clear.
In answer to your question, persistent storage (SQL, Internal\External storage, SharedPreferences) acts mostly the same by default, and Cache acts different.
Persistent storage - will be NOT be cleared in following scenarios: 1, 2, 3 - will be cleared in other scenarios.
Internal\External storage - can survive application removal (if configured appropriately), so, has possibility to NOT be deleted on 4 & 5, if you specifically handle this.
Cache - will be cleared in all scenarios (you may be able to access cache after application close in certain instances, but not reliably).
7* (ROM updates can retain application data using a backup solution, otherwise, would act same as 6)

Sqlite Database deleted when I clear data from Application

I have created Sqlite database in app. when I clear data from settings->applications->manage applications the Sqlite db removed. any suggestions to keep sqlite database as it is.
When you press Clear Data from the Android application manager its supposed to remove everything related to the app such as preferences, databases, caches etc the only thing that gets left is the app so when you re-launch it behaves as if it was just installed.
If you want to allow the user to clear the data but keep the database then there should be an option in the menu that removes the shared preferences but doesn't do anything with the database.
Hope this helps.
Android's SQLite is intented for local app data storage. When you opt to wipe your app's data, this data is wiped (as expected).
If you want to persist DB data, look into external storage (eg. the late Parse.com, or MS's Azure). You'll be making network calls, your local data will still be wiped, and you'll need to have a way to link your app back up with the external data post-local-wipe (eg. logging in) but your external data will survive an app data clear.
The "linking up" part can be mitigated as well depending on your use case, eg. Google Play Games' data services is tied to your Google Play id and will resync after an app wipe.
Why would you want to keep the data when the user wants to clear everything.
It is not suggested you keep the db.
I would suggest you use the sd card to store images/text files with the adequate permission from the user.

Categories

Resources