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.
Related
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.
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.
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.
Thanks for previous replies,
is it possible to delete the stored content from sqlite once re-install the application. I am storing data in database, once i re-install the same app again, the previous data still stores in sqlite. i want to delete the stored content while re install the app. i am not sure about this.
This seems like a hack and maybe not the actual answer, but can you -- with each new version of your app -- increment an identifier (from 10 to 11) in the code and then check against a stored preferences containing that identifier. If you have a constant in your code that is higher than the identifier stored on the previous device, then you can clear the database to whatever you think its state should be. Then with each new released version of the app you increment this number..
Edit: In API level 9 and higher, you can -- whenever your app starts up -- write the date in which the app was installed (see here for an explanation on how to find the install date). If you check that it was installed after the date which is written, kill the data!
Thanks for all replies,
I maintain the Version code for all the builds, once i re-install the application, i check the version, if i found the version changes, i simply remove the DB. this case only applicable for overriding the install of APk(ie without un-installing the application). When we made a uninstall, the internal data and sqlite for the application ill automatically deleted.
sqlite databases are stored as files on your file system, to delete the data. You just need to delete the file.
What you'd want to do is setup some way of detecting if the app is being run for the first time, if this is the first time the app has been run then check the database exists, if it does delete it. Then recreate the database as empty.
Or you could go through and remove all the data in each table/drop each table in the database on the first run if the database exists.
Read here : Detect Android app upgrade and set Application class boolean for show/hide of EULA
Create a UpgradeBroadcastReceiver of your own that will run the delete instructions you want and register it in your manifest file.
When you delete the app, then the database should be deleted too. Unless you go to some trouble to keep it. If you simply update the app, then the data should be kept. If you need to delete the data upon reinstall, try this:
Every time you start an Activity, call PackageManager.getPackageInfo() and check lastUpdateTime. Compare it with a time stamp that you store in the database or a shared preference. If lastUpdateTime is newer, delete the your database.
application SharedPreferance is deleted on un-installing the app, so save a boolean to determine if the application is running for the first time or not.
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()