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.

1
  • 1
    accepted answer is wrong!! Can you please change that. android:manageSpaceActivity=".ActivityOfMyChoice" to launch activity you want to open. See below answers Apr 21, 2015 at 16:08

4 Answers 4

136

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.

4
  • 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 <application> 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().
    – user942821
    Jan 11, 2012 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. :) Sep 22, 2015 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 :) Nov 26, 2015 at 10:51
  • 1
    Working on Android 5.+ and Android 6, Thanks.
    – Aspicas
    Mar 23, 2016 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!

4
  • 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, 2015 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. :) Sep 22, 2015 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. Feb 4, 2017 at 16:44
  • The <activity> should also have android:exported="true" Nov 3, 2022 at 0:10
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)

7
  • thanks for your answer, so is there any way to prevent database from user?
    – Neo
    Jun 30, 2011 at 9: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, 2011 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, 2011 at 6:29
  • 13
    -1: that is wrong. See the answer below. Also you can use: android:allowClearUserData="false" attribute in your manifest.
    – Borg8
    Sep 1, 2013 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 .groups.google.com/forum/#!topic/android-developers/hrW8OI_u2T4
    – dharmendra
    Sep 22, 2014 at 12:55
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.