How to prevent a shared preference from being clear in setting

时光总嘲笑我的痴心妄想 提交于 2019-12-17 19:45:51

问题


I don't want to let Settings clear my app sharedPreference by anyone: is it possible?


回答1:


hey buddy i found an answer here is a way to prevent data being cleared from Shared Preference

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

it will call my activity and the Activity is finish at creation :)

public class ActivityOfMyChoice extends Activity {

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

        finish();

    }
}

Now you can press the "Manage space" as much as you want! :)

this method works 100% of the time.




回答2:


I found this similar question:

Shared preferences and files not saved to the SD card will be deleted when the app is uninstalled.

Files saved to the SD card will persists after uninstall, however, the user will have access to read, over-write, and delete these files at will.

If you need truly permanent storage the best way forward would be to store the data remotely on a server that you control.

If you don't care about data, then you can save it on SD Card.

EDIT

I think that BFil answer's is very good on this post, check it.

My final verdict

After long research (for my pure and insane curiosity!), there is no way (at the moment?) to preserve SharedPreference after clear on Settings: the only way is a server vault or Data Backup.




回答3:


add this tag in manifesto allowClearUserData=false. this code verified.



来源:https://stackoverflow.com/questions/20300166/how-to-prevent-a-shared-preference-from-being-clear-in-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!