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中啓動另一個對象。