Home » Android » java – Avoid deleting the data from database on clicking 'clear data' button in the app settings on android device

java – Avoid deleting the data from database on clicking 'clear data' button in the app settings on android device

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using database to store some data. But when user go to Apps Settings and press the 'clear data' button in the app settings, data in databases gets deleted how can avoid this. Can I make 'clear data' BUTTON foR my app in Apps settings as not enabled or any other suggestions….

How to&Answers:

Add android:manageSpaceActivity=".ActivityOfMyChoice"
to the application tag of your Manifest like:

<application
android:label="MyApp"
android:icon="@drawable/icon"
android:manageSpaceActivity=".ActivityOfMyChoice" >

Then instead of “Clear Data”, there is a button for “Manage Space” which launches ActivityOfMyChoice

ActivityOfMyChoice.java

public class ActivityOfMyChoice extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    finish();
}

So when we click the Mange activity it finish the Activity.

Answer:

There is no direct way to avoid any app data being cleared from what I know.

However, you could store your data on the external storage (SD card) in some file and at every app launch check if that file is there and merge it’s contents into the database. This will of course not work if the user manually deletes your backup file from the SD, inserts another SD, removes the SD or connects the device as a mounted drive to the computer and then launches the app.

Answer:

Android won’t ask whether your App like’s its data to be deleted or not. It’s a system functionality provided to user. User has power to totally uninstall the App.

So, create/open the database file at another location.

Data durability:

  1. App cache dir : deleted when internal memory is low.
  2. App data dir : deleted when user clears app data , app is uninstalled.
  3. External storage : user can delete anytime, user may also remove the storage.
  4. Backup manager : new versions of android offer Apps to backup data on cloud via BackupManager.
  5. Remote server : Stays in server until deleted.

Answer:

I think there’s a way but haven’t tried it yet. You can store your DB file in asset folder, check if it available, otherwise just do like this https://stackoverflow.com/a/9109728/265167 (pay attention on DataBaseHelper class). But do this way, you’ll lose all “update data”.

Answer:

Finally I found some solution, If you make application system administrator then its not possible to clear data and uninstall app