I have a question as I am getting an Error in my code which I know how to fix if I get the answer to this question. When you uninstall your app that has a SharedPreferences file storing some values, does the file get deleted too or do you need to delete it manually with the method?
Thanks in advance!
When you uninstall your app that has a SharedPreferences file storing some values, does the file get deleted too
Yes.
do you need to delete it manually with the method?
No, in large part because there would not be any method to do this. Your code is not called when your app is being uninstalled.
When you delete the app, the SharedPreference also gets deleted. Another option is, Go into your Settings, find the app and click on Clear Cache and Clear Data. When you reopen the application, it will open like it was newly installed.
The files are manually deleted, so you need not worry about deleting it.
The default SharedPreferences is stored at:
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
or at /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml.
On uninstall of the application, the data folder (containing the sharedpreferences) gets deleted. This happens on the Clear data as well (as mentioned in the above answer).
Hope this helps!
Related
I use the internal shared preferences to store key/values on an Android device. I wanted to have a look at this data directly using a file explorer but I cannot find where it is stored physically. Most apps have an own directory on Android/data but there is no folder for my Flutter app.
Do you have an idea where the data is stored locally?
Thanks in advance.
Edit (To make my problem clearer):
I need to access this data from outside app. A user of my app is not able to start the app anymore for some reasons. I want him to send me the internal data for debugging purposes.
So is it possible to fetch the related files in any way without the app self?
SharedPreferences are stored in an xml file in the app data folder, i.e.
/data/data/YOUR_PACKAGE_NAME/shared_prefs/
You can access these from Device File Explorer Tab inside the Android Studio IDE on the right side :
On native android its stored inside the data folder. The path will be something like
/data/data/<your_package_name>/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
On Flutter projects its stored inside the same folder but with fixed name FlutterSharedPreferences.xml
Something like
/data/data/<your_package_name>/shared_prefs/FlutterSharedPreferences.xml
Edited in response of updated question - 05/01/2023
If your app is relying heavily on sharedpreferences then I don't think there is a way to get it from outside your app. But I have an idea which you can try.
Try this package https://pub.dev/packages/flutter_logs.
This can create log files in file system. If your app is currently crashing after opening you can try to put this log code to write down the sharedpreferences inside the log file. Then you can setup some code to send the logs by email.
But there are few catches in this approach.
You have to set this code in initial screen like splashscreen and wait until this process is complete.
You have to send this updated apk and ask your user to install it.
I suggest you to implement this anyhow as in future you might get stuck in similar situation if you are relying on sharedpreferences.
Hope this helps.
I am making an Android app, and I will do some process at the first launch of the app.After the first launch I will not these process again. So, for doing this, I want to use SharedPrefences.
How long does it save data? When and how does its saved datas remove?
Data in SharedPrefences is saved until the user uninstalls the app, or clears the app cache
Data in SharedPrefences is saved as file in your app internal storage, its be removed in case:
unInstall app
clear you app data
call yourSharedPreferences.edit().clear().apply()
hope this helps
When you clear the cash memory form settings all the information will be deleted and start fully new. so it not exactly that you need to uninstall your app.
So sharedpreferences is basically store data in primitive way which mean it doesn't contain data directly instead it's reference.
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()
I want to clear data through coding for my application. Right now I am Clearing Data from Settings->Applications->Manage Application->My Application->Clear Data.
But I want to do it through coding.
Please help me if any one have answers.
Use Context's getFilesDir method to get the directory, and then delete all directory and data as explained here.
Using MByD's code to delete a dir, delete the folder /data/app/my.app.name ;-)
If the data is "Internal" to the application, it generally can't be cleared from any other app. I did read that you want to clear data of your app through coding but it wasn't clear whether you want to clear data from the same app.
You need to change the same application's code and instruct it to delete the data. But, if you're clearing from the same application you might want to specify which database table or file, for instance, you want to delete instead of just clearing all the data.
You'll find application data here: /data/data/packagename.appname/
Use this to delete the application files: deletefiles()