Search code examples
androidandroid-sqliteandroid-fileandroid-settings

Android prevent deleting application offline data or database on Clear data event


In case of network unavailability, storing some important information into the sqlite database and file on internal storage. When network available, it will sync to the server in background.

But when a user performs Clear Data, it deletes database and file stored on internal storage. Because of that, I am facing lot much data loss.

I already search on, how to catch clear data event and related below questions:

Also I can not store that data on external storage, because it can easily access by user using File managers and they can delete it manually.

So my questions are:

  1. How I can prevent my offline data from deleting?

  2. Is there any way to save offline data at secure place and access it only through application?


Solution

  • How I can prevent my offline data from deleting?

    You can't. It's the user's device, not yours, and so it is not your data — it is the user's data. The user can do what the user wants.

    Is there any way to save offline data at secure place and access it only through application?

    Internal storage prevents direct access to your files by the user or by other apps. It does not prevent the user from getting rid of those files, either through "clear data" or by uninstalling your app.

    Can we disable, Clear Data event for my application?

    Not really. On some versions of Android, you can get control when the user clicks that button. However:

    • Do not assume that all devices will support this
    • Device manufacturers might provide other means for the user to clear the data
    • The user can still uninstall and reinstall the app, which will clear the data