Bilbo Baggins
Bilbo Baggins

Reputation: 3692

Shared Preference Value lost when I close the app

I have an activity which has few TextViews.

I have created a service in async function which goes on in the background as an alarm and loads the data in the shared preference object. I load these values in those TextViews in the async.

I also have the same function in onStart(), which copies saved pref value in the TextView.

When I close the app (by sliding them out in ICS) and then try to open them again, the pref values are not being loaded in the TextView.

Why doesn't the update method work in onStart? Here is the code in onStart:

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

session is a static variable.

Thanks

Upvotes: 1

Views: 1677

Answers (3)

Bilbo Baggins
Bilbo Baggins

Reputation: 3692

The mistake was that I was creating the pref object in main activity. When I closed the main activity that object was destroyed. Async task kept running in the background and tried to access that object but failed since it was destroyed.

The trick was to initiate another object in Async separately.

Upvotes: 0

Corey Scott
Corey Scott

Reputation: 2440

You can change the PendingIntent in the notification by setting flags to reuse the previous instance of the Activity.

Something like:

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

You could also change the activity so that the SharedPreferences are loaded in onResume(), this method is run during activity creation and resuming.

Edit: Per your comments about losing the data I offer you the following additional code. I use this code in an AsyncTask and I don't have any problems with persistance.

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$
        }
    }

}

Upvotes: 0

Tamilselvan Kalimuthu
Tamilselvan Kalimuthu

Reputation: 1532

Load the preference values in onCreate() and onResume() instead.

Upvotes: 1

Related Questions