共享preferences值不被更新不被、preferences

2023-09-12 10:03:34 作者:旧颜

我尝试更新的共享preferences 的值,这里是我的code:

 编辑= preferenceManager.getDefaultShared preferences(本).edit();
edit.putString(设置preF_USERNAME +,txtuser。);
edit.putString(设置preF_PASSWORD +,txtpass。);
edit.commit();
 

现在的问题是,当我访问这个值,它没有返回更新后的值,它给我的共享preferences值。

但是,当我确认在 XML 文件中的数据,该数据在更新。

和重新启动我的应用程序后,我得到了更新后的值。因此,它要求我重新启动应用程序,以获取更新后的值。 那么,如何让这些更新的值一旦发生变化?

在此先感谢

这是我的整个code:

  @覆盖
    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);
        的setContentView(R.layout.main);
        CTX =这一点;

            状态= preferenceManager.getDefaultShared preferences(本).getString(设置preF_STATUS,Settings.DEFAULT_STATUS。); //获取旧值
        submit.setOnClickListener(新View.OnClickListener(){
          @覆盖
            公共无效的onClick(视图v){

                  在(CTX,真); //函数调用和值更新

                }
            }});

     状态= preferenceManager.getDefaultShared preferences(本).getString(设置preF_STATUS,Settings.DEFAULT_STATUS); //这应该给我一个更新的值,但给旧值

    }
    公共静态布尔上(上下文的背景下){
        返回preferenceManager.getDefaultShared preferences(上下文).getBoolean(设置preF_ON,Settings.DEFAULT_ON。);
    }

    公共静态无效(上下文的背景下,布尔上){
            如果(上)Receiver.engine(上下文).isRegistered(); //
        }




**********在reciver文件***********
公共无效isRegistered){
        编辑器编辑= preferenceManager.getDefaultShared preferences(Receiver.mContext).edit();
        edit.putString(设置preF_STATUS +,0。);
        edit.commit();
}
 
怎么批量修改MathType公式格式

解决方案

而不是使用 edit.commit()的; ,你应该使用编辑。适用(); 。 应用将立即更新preference对象,并保存新的值的异步的,所以让您阅读最新的值。

的commit()的

  

这是这个编辑器来提交您的preferences变回   共享preferences对象是编辑。这种原子执行   要求修改,替换目前无论是在   共享preferences。

     

请注意,当两个编辑在修改preferences的同时,   最后一个调用commit胜。

     

如果你不关心的返回值和你使用这个来自   您的应用程序的主线程,可以考虑使用适用于()来代替。

适用()

  

这是这个编辑器来提交您的preferences变回   共享preferences对象是编辑。这种原子执行   要求修改,替换目前无论是在   共享preferences。

     

请注意,当两个编辑在修改preferences的同时,   最后一个调用应用胜。      

与提交(),其中写到了preferences出持久性   存储同步,适用()提交它的改变内存   共享$ P $立即pferences而是启动异步承诺   磁盘和任何故障,您将不会收到通知。如果其他编辑器   这种共享preferences并定期提交(),而适用()是   依然突出,提交()将阻塞,直到所有的异步提交的   完成并提交本身。

     

由于共享preferences实例是一个进程内的单件,这是   安全的替代(中犯任何实例)与适用(),如果你是   已经忽略返回值。

     

您不必担心Android的组件的生命周期及其   交互应用()写入磁盘。该框架可确保   在飞行磁盘写入的应用()完整的开关状态之前。

I am trying to update the values of SharedPreferences, here is my code:

edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putString(Settings.PREF_USERNAME+"",txtuser);
edit.putString(Settings.PREF_PASSWORD+"",txtpass);
edit.commit();" 

The problem is that when I am accessing this values, it is not returning updated values, it gives me a value of SharedPreferences.

But when I am confirming the data in XML file ,the data updated in that.

And after restarting my application I am getting that updated values. So it requires me to restart the application to get updated values. So, how to get those updated values once it changes?

Thanks in advance

Here is my whole code:

@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        ctx=this;

            status=PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_STATUS, Settings.DEFAULT_STATUS);// get old value
        submit.setOnClickListener(new View.OnClickListener() {
          @Override
            public void onClick(View v) {

                  on(ctx,true);//  function will call and value is updated

                }
            }});    

     status=PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_STATUS, Settings.DEFAULT_STATUS);// this should give me a updated value but gives old value

    }
    public static boolean on(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_ON, Settings.DEFAULT_ON);
    }

    public static void on(Context context,boolean on) {
            if (on) Receiver.engine(context).isRegistered(); //
        }




**********in reciver file***********
public void isRegistered ) {
        Editor edit = PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).edit();
        edit.putString(Settings.PREF_STATUS+"","0");
        edit.commit();
}

解决方案

Instead of using edit.commit();, you should use edit.apply();. Apply will update the preference object instantly and will save the new values asynchronously, so allowing you to read the latest values.

commit()

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call commit wins.

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

apply()

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call apply wins.

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.

As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with apply() if you were already ignoring the return value.

You don't need to worry about Android component lifecycles and their interaction with apply() writing to disk. The framework makes sure in-flight disk writes from apply() complete before switching states.