Android的 - 在AsyncTask的onPostExecute设置共享preferences()不总是设置? [英] Android - shared preferences set in an AsyncTask onPostExecute() not always set?

查看:131
本文介绍了Android的 - 在AsyncTask的onPostExecute设置共享preferences()不总是设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,我自己的测试期间的工作时间98%和100%,所以我真的不能重现其他问题不是让用户设备遇到此问题。

I have some code that works 98% of the time, and 100% during my own testing so I can not really reproduce the problem other than having user devices experience this issue.

我onPostExecute()设置一个参数是这样做的:

What I do in onPostExecute() is set a parameter like this:

   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( AddProblemActivity.this);
            prefs.edit().putString("recent_problem_id", result ).commit();

,然后进入下一个活动:

and then go to the next activity:

            Intent myIntent = new Intent(AddProblemActivity.this, ProblemActivity.class);
            AddProblemActivity.this.startActivity(myIntent);

,然后试图获取参数有这样的:

and then try to get that parameter there like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( 
              ProblemActivity.this);         

    // display a loading message before problem loads.
    String recent_problem_id = prefs.getString( "recent_problem_id" , null );

    if ( recent_problem_id == null )
    {
        // Sometimes it is null!            
    }

会有人知道为什么会这样?

Would anyone know why this happens?

谢谢!

推荐答案

如果你正在尝试将数据传递到一个新的活动,为什么不把它作为额外的意图的字符串?然后,得到了新的活动的意图字符串。如果您还需要保存它,你可以在新的活动的onCreate()它拉它的意图后也这样做。

If you are trying to pass the data to a new activity, why not put it as a String extra in the intent? Then, get that String from the intent in the new activity. If you still need to store it, you can do so in the new activity's onCreate() after it pulls it from the intent.

Intent myIntent = new Intent(AddProblemActivity.this, ProblemActivity.class);
//Add results here
myIntent.putExtra("RecentProblemId", result);
AddProblemActivity.this.startActivity(myIntent);

然后,在你新的活动的OnCreate,做的:

Then, in the onCreate of your new Activity, do:

String recentProblemId = getIntent().getStringExtra("RecentProblemId");

现在,如果你还需要存储这些信息,这样做:

Now, if you still need this information stored, do:

if(recentProblemId != null){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( 
              ProblemActivity.this);         
    prefs.putString("recent_problem_id",recentProblemId).commit();
}

我知道这并不完全回答你的问题,为什么字符串并不总是被提交到preferences在onPostExecute()。然而,对于传递活动之间信息的最佳做法是通过意图和群众演员。

I know this doesn't exactly answer your question as to why the String isn't always being committed to the preferences in onPostExecute(). However, the best practice for passing information between activities is via Intents and extras.

我的,为什么它可能不总是对某些用户工作的猜测是,他们的设备都没有做新的活动开始前写入数据到共享preferences文件,并试图从同一文件中读取。希望这有助于。

My guess as to why it may not always work for some users, is that their devices are not done writing the data to the shared preferences file before the new Activity starts and tries to read from that same file. Hope this helps.

这篇关于Android的 - 在AsyncTask的onPostExecute设置共享preferences()不总是设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆