1

当人们第一次启动我的应用程序时,他们必须选择一个国家。此值存储在 sharedpreferences 中,并且可以正常工作,直到他们收到推送通知并使用快捷方式访问应用程序中的活动。当返回到 android 仪表板并启动应用程序(单击图标)时,应用程序运行良好,但当他们决定强制关闭应用程序并返回应用程序时,sharedpreferences 被完全清除。此问题仅在使用 android > API 13 时存在。我需要在快捷方式活动中创建一个 sharedpreferences 实例还是我的代码有问题?

public void saveCountry(Context context, String countryCode) {

    SharedPreferences settingsActivity  = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settingsActivity.edit();
    editor.putString("key_country", countryCode);
    editor.commit();

    setDefaultChannels(context);
}

public String getCountry(Context mContext) {


    SharedPreferences settingsActivity  = mContext.getSharedPreferences("preferences", Context.MODE_PRIVATE);

    String country = settingsActivity.getString("key_country", null);
    return country;
}
4

0 回答 0