0

I've kept a static shared preference to access the values from multiple activities.

Now, I've set an alarm to go off at a certain time. Now in that Broadcast Receiver, I'm trying to access the shared pref variable.

It has already been initialized in another activity and returns the proper value there.

But in this Broadcast Receiver it doesn't give the actual value. It gives the uninitialized value.

Since it is static shouldn't the value remain same?

Here is the shared preference class.

package com.footballalarm.invincibles; 

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class SessionManagement {
// Shared Preferences
public static SharedPreferences pref;

// Editor for Shared preferences
public static Editor editor;

// Context
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Shared pref file name
private static final String PREF_NAME = "invincibles_alarm";

// All Shared Preferences Key
public static final String PREF_MATCH = "match";

// Constructor
public SessionManagement(Context context){
    this._context = context;
    pref = _context.getSharedPreferences(getPrefName(), PRIVATE_MODE);
     editor = pref.edit();
     editor.commit();
     Log.e("Pref Match Value-constructor for pref", getValueMatch());
}

public static void fillValues(String match){
try{// Storing login value as TRUE
    editor.putString(PREF_MATCH, match);

    // commit changes
    editor.commit();
  Log.e("Pref Match Value-in fill values", getValueMatch());
}
catch(Exception e)
{Log.e("fillValues", e.toString());}

}   

public static String getValueMatch(){       
    return pref.getString(PREF_MATCH, "Loading Match"); 
}
public static String getPrefName() {
    return PREF_NAME;
}

}

I tried to log the output in other activities and it returns properly.

When I run the app and then close it before the alarm takes place, the program crashes with null-pointer exception since the Broadcast Receiver cannot access the shared pref.

I have tried this solution - SharedPreferences in BroadcastReceiver seems to not update? but I'm only using name in the manifest for the recievers.

This only happens if I close my app in ICS via the minimize menu.

Community
  • 1
  • 1
Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64

1 Answers1

1

Check this link:

Static variable loses value

Maybe static variables are loosing its value in your case .

static variables can loose value in the following cases:

1) Class is unloaded.

2) JVM shuts down.

3) The process dies.

Instead of using static variables and functions , try using a public class instead.

Hope it helps

EDIT 1:

Example code of using a public class for preferences instead of static methods

public class PreferenceForApp {
    Context context;
    SharedPreferences prefs;

    public PreferenceForApp(Context context) {
        this.context = context;
        prefs = context.getSharedPreferences(AllConstants.PREFS_NAME, 0);
    }



    public Boolean getIsDeviceValidated() {

        return prefs.getBoolean("Validated", false);
    }

    public void setIsDeviceValidated(Boolean value) {

        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("Validated", value);
        editor.commit();
    }


}

In your code add:

PreferenceForApp myPref = new PreferenceForApp(contxt);
myPref.getIsDeviceValidated();

Useful related links:

Android static object lifecycle

Why are static variables considered evil?

Android : Static variable null on low memory

EDIT 2

TEST when is your static variable loosing value :

You can test this with a few lines of code:

  • print the uninitialized static in onCreate of your activity -> should print null

  • initialize the static. print it -> value would be non null

  • Hit the back button and go to home screen. Note: Home screen is another activity.

  • Launch your activity again -> the static variable will be non-null

  • Kill your application process from DDMS(stop button in the devices window).

  • Restart your activity -> the static will have null value.

I referred to this link Android static object lifecycle

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • In my case it happens when I manually close the app. @Rachita is there any way to keep it persistent? – Bilbo Baggins Jul 15 '13 at 08:33
  • I think static variables are loosing value due to one of the above reasons. As i suggested ,you should use a public class for preferences and public methods.I will add code in my edit – Rachita Nanda Jul 15 '13 at 08:39