【问题标题】:SharedPreference Remove when we exit the applicationSharedPreference 当我们退出应用程序时移除
【发布时间】:2011-09-01 11:30:41
【问题描述】:

我想删除 sharedPreference 详细信息;我确实喜欢这样做,但不起作用:

SharedPreferences  myRoutes = this.getSharedPreferences("myDefalutRoute", MODE_PRIVATE);
public void onExitAction(View botton){
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    finish();    

    SharedPreferences myPrefs = this.getSharedPreferences("myLogedPrefs",MODE_WORLD_READABLE);
    myPrefs.edit().remove("myLogedPrefs");
    myPrefs.edit().clear(); 
    myPrefs.edit().commit();

    myRoutes.edit().remove("myDefalutRoute");
    myRoutes.edit().clear(); 
    myRoutes.edit().commit();

    moveTaskToBack(true);

}

退出应用程序并转到 data\data\package\myLogedPrefs.xml 后包含值。

我们如何删除 SharedPreferences 数据?

请帮帮我..

提前致谢;

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:
    @override
    public void onDestroy()
    {
        super.onDestroy();
        SharedPreferences myPrefs = this.getSharedPreferences("myfile",MODE_WORLD_READABLE);
        myPrefs.edit().remove("myfile");
        myPrefs.edit().clear(); 
        myPrefs.edit().commit();    
    }
    

    【讨论】:

      【解决方案2】:

      你确定 onExitAction() 被调用了吗?你从哪里叫它。放入一些日志记录以确保它被调用。正如 J_Andr 所提到的,您可能希望移动此代码或从应用程序初始 Activity 的 onDestroy 方法调用该函数。

      【讨论】:

      • 感谢您的帮助。我应该在初始页面中放入 onDestroy() 吗?
      • 是的 - 无论您在清单中定义为主要的 Activity。
      【解决方案3】:

      尝试将这些删除命令放入 onDestroy()。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多