1

我有一个活动只有很少的TextViews。当我关闭应用程序时共享偏好值丢失

我在异步函数中创建了一个服务,它在后台继续作为警报并将数据加载到共享首选项对象中。 我在异步的那些TextView中加载这些值。

我还在onStart()中具有相同的功能,它在TextView中复制保存的pref值。

当我关闭应用程序(通过在ICS中滑出它们)然后尝试再次打开它们时,pref值不会被加载到TextView中。

为什么update方法在onStart中不起作用?这里是onStart中的代码:

if(AsyncTaskTestActivity.session != null) 
    { 
    Log.e("SessionManagement", "onStart"); 
    updatePref(); 
    } 
else{Log.e("SessionManagement", "falseonStart");} 

session是一个静态变量。

感谢

回答

0

您可以通过设置标志重用活动的前一个实例改变的PendingIntent通知。

喜欢的东西:

Intent intent = new Intent(context, MainActivity.class); 
intent.setFlags(Intent.FLAG_CLEAR_TOP); 
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT 

你也可以改变活动,使得SharedPreferences在加载的onResume(),这个方法是创建活动和恢复过程中运行。

编辑:根据您对丢失数据的意见,我为您提供以下附加代码。我在AsyncTask中使用此代码,并且我没有任何持久性问题。

public final class PreferenceUtils 
{ 
    private static final String TAG      = PreferenceUtils.class.getSimpleName(); 

    public static final String SESSION_ID    = "sessionId";       //$NON-NLS-1$ 
    public static final String NAME      = "name";        //$NON-NLS-1$ 

    private PreferenceUtils() 
    { 
     // enforcing singleton 
     super(); 
    } 

    /** 
    * Set sessionId in app preferences to the supplied value. 
    * 
    * @param context 
    * @param sessionId 
    */ 
    public static void setSessionId(final Context context, final String sessionId) 
    { 
     final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 
     final Editor editor = settings.edit(); 
     editor.putString(PreferenceUtils.SESSION_ID, sessionId); 
     editor.commit(); 

     if (BuildConfig.DEBUG) 
     { 
      Log.d(PreferenceUtils.TAG, "Setting sessionId: " + sessionId); //$NON-NLS-1$ 
     } 
    } 

    /** 
    * Get the current session id 
    * 
    * @param context 
    * @return session id or null on not activated 
    */ 
    public static String getSessionId(final Context context) 
    { 
     final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 
     return settings.getString(PreferenceUtils.SESSION_ID, null); 
    } 

    /** 
    * Get current name stored in the app preferences 
    * 
    * @param context 
    * @return 
    */ 
    public static String getName(final Context context) 
    { 
     final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 
     return settings.getString(PreferenceUtils.NAME, null); 
    } 

    /** 
    * Set name in app preferences to the supplied value. 
    * 
    * @param context 
    * @param name 
    */ 
    public static void setName(final Context context, final String name) 
    { 
     final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); 
     final Editor editor = settings.edit(); 
     editor.putString(PreferenceUtils.NAME, name); 
     editor.commit(); 

     if (BuildConfig.DEBUG) 
     { 
      Log.d(PreferenceUtils.TAG, "Setting name: " + name); //$NON-NLS-1$ 
     } 
    } 

} 
+0

显然,主要的问题是,一旦我销毁活动并再次打开它,共享首选项值对象就会丢失,我无法使用它来访问数据。 – 2013-02-23 05:29:08

+0

我已经添加了更多基于AsyncTask代码的代码,我使用 – 2013-02-24 07:11:36

+0

,因此根据您的说法,我可以创建两个不同的对象,这两个对象都可以使用相同的共享偏好? 当前共享的前置对象如果其创建的活动被破坏,就会死亡。其他活动则无法访问它。 – 2013-02-25 08:31:15

0

负载在onCreate()onResume()代替偏好值。

+0

我已经在异步文件(不在主活动中)初始化共享pref的值。所以当我关闭应用程序并尝试再次打开主要活动时。它无法找到共享的pref对象。 – 2013-02-23 05:30:28

0

错误在于我在主要活动中创建了前期对象。 当我关闭对象被破坏的主要活动时。 异步任务保持在后台运行,并尝试访问该对象,但因为它被销毁而失败。

诀窍是分别在Async中启动另一个对象。

相关问题