42

I am working on android 2.2, In a Application tab of android manifest provide the option of "Allow clear data" to set true or false. But after setting it to False, my application can't disable the button of Clear data in application info of Manage application. I am facing this problem when application contains database in Data/Data/packge-name/databases/.

I have to protect my application database from user.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Neo
  • 423
  • 1
  • 5
  • 4
  • 1
    accepted answer is wrong!! Can you please change that. android:manageSpaceActivity=".ActivityOfMyChoice" to launch activity you want to open. See below answers – Shirish Herwade Apr 21 '15 at 16:08

4 Answers4

137

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

As far as I have been able to tell, this works 100% of the time.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • 1
    Solution of Jakar: Great :-). Work on... emulator and Acer Liquid Mini, Android 2.1. Just a note that you place that property inside tag `` of `AndroidManifest.xml`. It will even open sub activity, except that the system will call it as normal way, so don't design it such as transferring data to it when you call `startActivity()`. –  Jan 11 '12 at 23:57
  • 2
    Note: Try keeping the complete package name. I tried like .activities.ManageSpaceActivity which caused settings to crash in Lollipop. However giving the complete package name worked. :) – Deepak Senapati Sep 22 '15 at 12:25
  • @Saty I don't remember which ones I tried with but you are free to check and let me know if you faced any issues :) – Deepak Senapati Nov 26 '15 at 10:51
  • 1
    Working on Android 5.+ and Android 6, Thanks. – Aspicas Mar 23 '16 at 12:53
49

AndroidManifest.xml

<application
    android:manageSpaceActivity="[packageName].ManageSpaceActivity"
    ...
    ...
>


  <activity
    android:name="[packageName].ManageSpaceActivity"
    android:screenOrientation="portrait" />

it will call my activity, but:

public class ManageSpaceActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        finish();

    }// onCreate
}

and the Activity is dead at creation, I love tricks :)

Now you can press the "Manage space" as much as you want! :)) - if you need you can do custom data / cache delete at ManageSpaceActivity, but you can keep your data, which do you want.

Up votes on Jakar answer too pls!

Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45
  • 4
    Also, don't just blindly finish the manage space activity in an app that gets published. 1) because it provides a bad user experience if you just immediately `finish()`. And 2) it may void Google's T&C's for GPlay store (i'm too lazy to check, though). – Reed Mar 17 '15 at 15:03
  • 1
    Note: Try keeping the complete package name. I tried like .activities.ManageSpaceActivity which caused settings to crash in Lollipop. However giving the complete package name worked. :) – Deepak Senapati Sep 22 '15 at 12:26
  • 2
    *I love tricks :)*? You should hate them. If the API is well designed you should not need any tricks. Forcing a framework or library like this means that you need something else. The problem with android is that there is nothing else, so you are forced to do tricks. – Iharob Al Asimi Feb 04 '17 at 16:44
  • The `` should also have `android:exported="true"` – gardenapple Nov 03 '22 at 00:10
3

Just a trick.

<application
    android:manageSpaceActivity="{packageName}.ManageSpaceActivity"
>

In this scenario Android OS will show a Manage Space button in place of clear data.

Upon clicking it will open ManageSpaceActivity.

But

    <application
        android:manageSpaceActivity=".AnyActivity"
    >

If you do this. It will disable the Clear data button.

Trick is to write the Activity name without the full package name.

Nauman Khaliq
  • 909
  • 5
  • 2
3

There is no way to prevent the user from clearing your app data. The manifest option you mention is intended for system apps only and you, as a developer, have no way to install system apps.

Please see this discussion for details - particularly this response from Diane Hackborn (Android framework engineer)

RivieraKid
  • 5,923
  • 4
  • 38
  • 47
  • thanks for your answer, so is there any way to prevent database from user? – Neo Jun 30 '11 at 09:37
  • 1
    Not if it's stored on the device - it will be deleted when the app is uninstalled, and the user will always be able to delete via the clear data option. If the data is read-only, place it in the app resources and restore it if necessary when the app is executed. If the data needs to change or be updated, I think you'll need to store it remotely instead, and then you'll have to deal with accessing it over the internet. – RivieraKid Jun 30 '11 at 12:23
  • If you observe that clearing data of Android Market also resets it to initial stage. so If there is some way then they should implemented it in market. – AZ_ Oct 18 '11 at 06:29
  • 13
    -1: that is wrong. See the answer below. Also you can use: android:allowClearUserData="false" attribute in your manifest. – Borg8 Sep 01 '13 at 11:48
  • 1
    @Borg8 you can not use that command directly , those options are for only system app like Mediaprovider and in order to use that option you have make you own hardware powered by android os. See below discussion which tell you to dont mess with that option .https://groups.google.com/forum/#!topic/android-developers/hrW8OI_u2T4 – dharmendra Sep 22 '14 at 12:55
  • -1: Wrong answer. you can do it by android:manageSpaceActivity. https://developer.android.com/guide/topics/manifest/application-element – Seyyed Oct 27 '19 at 09:21
  • The links to the group discussion no longer work. They're marked as banned. – Nikos Hidalgo Apr 08 '20 at 08:14