Clear data in android using coding - android

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()

Related

How to prevent remove objectbox db from clear app cache

I use the ObjectBox in my kotlin project. Users can erase the database by clearing the application Data from the Android settings.
I want to prevent database removal or change ObjectBox Store mode to SQLite!
thanks
In case you want to make user data persistent you definitely should save and check DB copy on your server.
First of all, as you probably know, this is "normal" behavior on Android. When you delete an app's data, you have to start from scratch. E.g. all local data lost and log-in again etc. I'd guess that 0.01% of users would actually know about it and actually do that.
Anyway, yes, the question is if you want to extend data scope to cloud/server... That's a different topic though. ObjectBox will offer data synchronization later this year.
Alternatively, there's also Android's backup function: https://developer.android.com/guide/topics/data/autobackup.html

Does the SharedPreferences File get deleted when you Uninstalled?

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!

Clear Cash in android application

I have android application, I use SQLite data base in my application, I have a question about data base in setting -> application manager if I select clear data or clear cash, our data base deletes, but I need data base. For example user remove data base suddenly after that my program does not work, my program is related to data base, how do I do, how can I resolve my problem?
You will need to find some way to backup your database to the SD card. You could just dump it as a CSV-type file, which would exactly mimic the structure of a database, or else create some other kind of format which would work better for you. And then in the case that the database is cleared, you will need to parse the text file you created as the backup, and put it all back into the database.

Android - SQLite

I am trying to build an app that will use an SQLite database. My question is once the database is created, will all data still be there once the app is closed, i.e.: the database won't be overwritten when the app is restarted?
You, as a developer are the only one who have access to this database, thus it is only your code which will be allowed to change the state or content of this database.
Yes, the data will remain there.
The data will remain there because of your default create method. There is a status code. You can use SQLScout http://www.idescout.com/ to visualize the table and watch its change step by step.
Yes, data will remain in your application when you close the application. When you run the application the database create in the application.
But yes, if the user uninstalls, the application then user will lose the application. On the other hand, if you want to save the data when the application is uninstalled, then it's better to create the database in SDCard.

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