无法更改SharedPreferences中的值

时间:2018-07-09 14:30:27

标签: android sharedpreferences android-espresso

我正在为我的应用程序编写UI测试,并且需要在每次测试之前在SharedPreferences中设置一些值。

这是我在每次活动的家长中准备SharedPreferences的方式

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

    //set up sharedPreferences by the default file
    if (mSharedPreferences == null) {
        mSharedPreferences = getSharedPreferences(Globals.PREFS_FILENAME, MODE_PRIVATE);

        //check if preferences are loaded
        if (mSharedPreferences.getString(Globals.ARTICLES_DETAIL_FILENAME, null) == null)
        {
            //not loaded, load default values
            loadDefaultSetting();
        }
    }

在测试中,我尝试使用类似的方法。

@Before
public void init() {
    Intents.init();

    context = getInstrumentation().getTargetContext();
    sharedPreferences = context.getSharedPreferences(Globals.PREFS_FILENAME, MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(Globals.ALLOW_QUANTITY_CHANGE, false);
    editor.putBoolean(Globals.LOOP_ROWS_DIALOG, false);
    editor.commit();
}

但是,当活动要求Globals.ALLOW_QUANTITY_CHANGE时,它将获得true,这是默认的未更改值。 我在做什么错了?

0 个答案:

没有答案