Shared Preference Value updated with installation - android

Im trying to make a currency converter, and so I get the values from the internet and if the user turns the internet off the values are being saved in the shared preferences so that the user can go on using the converter without the internet.
The problem is first installation. How can I make it work in the way that shared preferences from the last usage install themselves within the application. Let me give you an example:
1) I get the current rates, save them using shared preferences.
2) I uninstall my app
3) Install my app again
4) Rates in my shared preferences are the ones from point 1.

So your problem is that your Prefs are remain after you deleted and installed your application again? Looks like you should add
android:allowBackup="false"
in your Manifest in the <application> block, because I guess your Prefs are stored in your Google Drive :)
From official documentation:
Whether to allow the application to participate in the backup and restore infrastructure. If this attribute is set to false, no backup or restore of the application will ever be performed, even by a full-system backup that would otherwise cause all application data to be saved via adb. The default value of this attribute is true.

Related

Will SharedPreferences be automatically deleted from the device once the app is deleted?

I'm building a flutter-firestore live POS system and have largely stayed away from using local data persistence like SharedPreferences. My concern is that I don't know if I need to clean up local resources once app is deleted or how to do so.
SharedPreferences is always deleted along with the app uninstall.
When you uninstall any application all the changes the application have made in your internal memory are revoked, that means your SharedPreference files, Other data files, Database file, Application gets removed automatically by the Android OS.
This is a new marshmallow feature.
Add android:allowBackup="false" tag inside your object in your app manifest to disable this behaviour.
If android:allowBackup tag clashes with any other library you are using, you should add tools:replace="android:allowBackup" also.
SharedPreferences is deleted once app is uninstalled. In case of updates, SharedPreferences is not deleted. But remember one thing. always remember to include all the SharedPreferences values of the previous versions while launching an update. For example, you had put a few SharedPreferences values in the first version. So, in the next update, the users will have all the SharedPreferences values from the previous version. But if a user, who did not have the previous version, install the next version for the first time, he/she will not have the SharedPreferences values from the previous version. So, make sure to include all the previous SharedPreferences values in your updates and put them accordingly if they are not found in the device.

Can you change Shared Preferences from outside your app?

Is there a way user could mess with the shared preferences values without the help of my app? E.g. can I store license details here and not worry about user extracting and copying the license key?
Its really easy to access the shared preferences.
All you need is a file explorer with root access, they are saved in an xml file in /data/data/YOUR_APP_NAME/shared_prefs/YOUR_APP_NAME_preferences.xml
For licencing you should either use google play's licence check or implement your own checking on a remote server.
If the context of the shared preferences are private (you define it when you create) only with root access it is possible to access them, without being the application who created.
Shared preferences are stored inside the app's space, when you uninstall the app, the preferences are also gone.
So normally users cannot get these values, they are stored privately.
However, with root access and a simple memory search app, a user could be able to access the data. (It is always better to store such things server side.)

Removal of shared preference on update of android application through play store updates or auto updates?

When I update application through popup implemented in application code on change of version shared preference persist but when update using auto-update or from updates available in play store shared preferences got removed?
You should have a look at SharedPreferencesBackupHelper.
Simply put, it backs up your SharedPreference data when ever your app is uninstalled, or when ever you would like to back up your SharedPreferences.
Additionally, this question also addresses your specific problem.
To manually request a restore, you call the requestRestore() method. See this for reference.
Restore the calling application from backup. The data will be restored
from the current backup dataset if the application has stored data
there, or from the dataset used during the last full device setup
operation if the current backup dataset has no matching data. If no
backup data exists for this application in either source, a nonzero
value will be returned.

GooglePlay update App and wipe data

I need to update an application on Google-Play but I WANT that all the old settings saved via Shared Preferences and Local Storage will be wipe instead of being kept (because I have change the main structure of the data that will be saved and it's not compatible anymore with the old one).
How can I do that programmatically?
Thank you in advance for the future answers.
Heres how I would do it:
If you haven't already, create an Application class that extends android.app.Application. Make sure you declare this in your manifest. Read more about this here - http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android.
Then in your applications onCreate method, check the version of the app, and if it matches the new version, run your methods to clear out the shared preferences,local storage.
You can then set a boolean preference that the 'upgrade' has been completed,so your app doesn't try to clear out the data every time it's run.

Saving preference that can be read and modified by another Application

I built an application that will download and install an APK from a WebService using the technique described here:
Install Application programmatically on Android
During this installation, the Webservice sends a 'flag' that indicates if the SQLite database from the application that is being updated should be deleted or not during it´s first run.
Is there any way to set a "Global Preference" that could be read (if the flag is true, the database should be deleted) and cleared (it should be set to false after deletion to avoid deleting the database all times that app is started) during the first usage of the updated app, without saving it to the SDCard?
I know how to read the preferences from the app that is being updated but, I did´t realize how to modify these preferences from another app.
Thanks a lot.
SharedPreferences are unique to each App/APK - no way to share them that I'm aware of and no 'Global' equivalent.
If you want to share data, the solution is usually some sort of ContentProvider, but that relies on both apps running at the same time.
If you only want to hand-over a token or state, I'd suggest writing a file onto the SDCARD is probably the simplest option?
Here is a tutorial on how to do it.
Basically you have to use MODE_WORLD_WRITEABLE for the prefs file.
To get the context for the other package you use createPackageContext()

Categories

Resources