How to pass application context from Singleton class to SharedPreferences? I have a fragment and a GridView inside its onActivityCreated(Bundle savedInstanceState) method , on item-click, I am getting NullPointerException in logcat:
03-30 05:12:54.784: E/AndroidRuntime(1950): FATAL EXCEPTION: main
03-30 05:12:54.784: E/AndroidRuntime(1950): java.lang.NullPointerException
03-30 05:12:54.784: E/AndroidRuntime(1950): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
03-30 05:12:54.784: E/AndroidRuntime(1950): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
03-30 05:12:54.784: E/AndroidRuntime(1950): at com.example.duaapp.utility.SharedPreferencesSupplication.save(SharedPreferencesSupplication.java:35)
Singleton Class
public class SingletonClass {
public static Context applicationContext;
public static int fontSizeMin = 17;
public static int fontSizeMax = 35;
public static final String keySelVerseFromList = "keySelVerseFromList";
public static final String keyFavVerses = "keyFavVerses";
public static final String keyListOfVerses = "keyListOfVerses";
public static final String keyIsFavSelected = "keyIsFavSelected";
}
SharedPreferences
public class SharedPreferencesSupplication {
public void save(String valueKey, String value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putString(valueKey, value);
edit.commit();
}
public void save(String valueKey, int value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt(valueKey, value);
edit.commit();
}
public void save(String valueKey, boolean value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(valueKey, value);
edit.commit();
}
public void save(String valueKey, long value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putLong(valueKey, value);
edit.commit();
}
}
On gridview_item_Click, whenever new SharedPreferencesSupplication().save(SingletonClass.keyIsFavSelected, false); is called, the app crashes and nullpointer exception is raised in logcat. Where am I going wrong?
Never use static references to Context in Android, this will cause you an important memory leak since Context has references to all the application resources.
Most of the Android UI classes have a dynamic reference to the context, in fragments you can do getActivity(), in views getContext() consider using those instead of context singletons
More information about this here
While Guillermo Merino concern is valid, an application Context used in a singleton class should not be automatically considered a memory leak.
Calling context.getApplicationContext() will, regardless of where or how it is accessed, always return the same instance from within your process. It will be "alive" for at least as long as the singleton will, therefore holding a reference to it should do no harm.
That said, it might not be enough to perform certain operations, as Dave Smith describes in his blog post.
Since OP is trying to access default SharedPreferences, this could be achieved using just application Context, e.g.:
// Custom Application class.
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
SingletonClass.INSTANCE.init(getApplicationContext());
}
}
// Singleton.
public enum SingletonClass {
INSTANCE;
private Context applicationContext;
public static int fontSizeMin = 17;
public static int fontSizeMax = 35;
public static final String keySelVerseFromList = "keySelVerseFromList";
public static final String keyFavVerses = "keyFavVerses";
public static final String keyListOfVerses = "keyListOfVerses";
public static final String keyIsFavSelected = "keyIsFavSelected";
// Make sure you only call this once - from MyApplication.
public void init(final Context context) {
applicationContext = context.getApplicationContext();
}
public Context getApplicationContext() {
if (null == applicationContext) {
throw new IllegalStateException("have you called init(context)?");
}
return applicationContext;
}
}
I don't believe this would cause any memory leaks, but if I'm mistaken, please do correct me.
Related
I have a PrefUtils class like this:
public class PrefUtils {
public PrefUtils() {
}
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences("APP_PREF", Context.MODE_PRIVATE);
}
public static void storeAccessToken(Context context, String access_token) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("Access_Token", access_token);
editor.commit();
}
public static String getAccessToken(Context context) {
return getSharedPreferences(context).getString("Access_Token", null);
}
}
In my MainActivity I have a static method and I should use a static Context for it :
public class MainActivity extends AppCompatActivity {
private static Context mcontex;
// Is It right To Add Context To static Variable???
#SuppressLint("CheckResult")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_method();
}
public static void my_method(){
String access_token =PrefUtils.getAccessToken(mcontex);
// more code ...
}
}
Is it right to add Context to a static variable?
Android Studio says: "Do not place Android Context class in static field".
Does this cause an error like memory leak? And if answer is Yes, what is the right way?
Pass the Context as a parameter to the method rather than keeping the context in a static variable.
public static void myMethod(#NonNull Context context){
String access_token =PrefUtils.getAccessToken(context);
// more code ...
}
The downside of having a static context variable is that there is no guarantee that the non-static onCreate() will have been called before some static initialization code tries to fetch your static Context object. That means your calling code will need to be ready to deal with null values.
I wanna refactor my project and it has a lot of unnecessary code with hardcoded string. So I wanted to create singleton SharedPrefsManager, i will use Application context. does it occur memory leak cause of static SharedPrefs in my manager class.
public class SharedPrefsManager {
private static SharedPrefsManager sharePref = new SharedPrefsManager();
private static SharedPreferences sharedPreferences;
private SharedPrefsManager() {}
public static SharedPrefsManager getInstance(Context context) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
}
return sharePref;
}
}
Holding SharedPreferences in static field doesn't provoke memory leaks because Preferences doesn't contain Context itself
No, it will not get any memory Leak if you use an application context. Create the SharedPrefsManager as a singleton and Initialize from your Application class. so that you can access from anywhere
Create like this,
public class SharedPrefsManager {
private static SharedPrefsManager mInstance= null;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor prefsEditor;
private SharedPrefsManager(Context context) {
sharedPreferences = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
prefsEditor = sharedPreferences.edit();
}
public static void init(Context context) {
mInstance = new SharedPrefsManager(context);
}
public static SharedPrefsManager getInstance() {
if (mInstance == null) {
throw new RuntimeException(
"Must run init(Application application) before an instance can be obtained");
}
return mInstance;
}
/**
* To get the Stored string value in Preference.
*
* #param key
* #param defaultvalue
* #return stored string value.
*/
public String getStringValue(final String key, final String defaultvalue) {
return sharedPreferences.getString(key, defaultvalue);
}
/**
* To store the string value in prefernce.
*
* #param key
* #param value
*/
public void setStringValue(final String key, final String value) {
prefsEditor.putString(key, value);
prefsEditor.commit();
}
}
From Application class you can initialize like this,
public class StackApp extends Application {
#Override
public void onCreate() {
super.onCreate();
SharedPrefsManager.init(this);
}
}
So that you can use from any other class without context.
Ex: you can use like this,
SharedPrefsManager.getInstance().setStringValue("key", "value");
I have a problem where my shared preferences are not working in a class file.I am confused and not able to solve it.Below is my file globalfile which saves data as follows.
public class globalfile extends Activity {
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
public static final String Pwd = "pwdKey";
public static final String Email = "emailKey";
private static String global_username = "null/", global_pwd = "null/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
}
public String getusername() {
global_username = sharedpreferences.getString(Email, "");
return global_username;
}
public String getuserpwd() {
global_pwd = sharedpreferences.getString(Pwd, "");
return global_pwd;
}
public void setusername(String someVariable) {
global_username = someVariable;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Email,global_username);
editor.commit();
}
public void setuserpwd(String someVariable) {
global_pwd = someVariable;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Pwd,global_pwd);
editor.commit();
}
}
I first called setuserpwd() & setusername() then getuserpwd() & getusername() from another activity using object of class globalfile.But always returns null.although if I use this code without shared pref.it is working fine
create an instance of Activity with Activity class object
Technically, you can create an instance of an Activity class. but, activity instance would be useless because its underlying Context would not have been set up.
The rule is that you should never ever create instances of Android components (Activity, Service, BroadcastReceiver, Provider) yourself (using the new keyword or other means). These classes should only ever be created by the Android framework, because Android sets up the underlying Context for these objects and also manages the lifecycle.
I think your newly created activity object can't get actual context. so try to avoid your current flow. I suggest you to create a separate class that may use as factory class and I think your problem will solved.
Or another solution would be like this :
Context context = /*get application context*/
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
/*get value from this sharedPref*/
So you don't need to create activity class object and here you should avoid your previous getter method to get value.
change you globalfile to be a utility class and not an activity so it can be used really easily by other parts of your app (that aren't an activity but have access to an android context).
public class Global {
private final SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
public static final String Pwd = "pwdKey";
public static final String Email = "emailKey";
private static String global_username = "null/",
global_pwd = "null/";
public Global(Context context) {
this.sharedpreferences = context.getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
}
public String getusername() {
global_username = sharedpreferences.getString(Email, "null/");
return global_username;
}
public String getuserpwd() {
global_pwd = sharedpreferences.getString(Pwd, "null/");
return global_pwd;
}
public void setusername(String someVariable) {
global_username = someVariable;
sharedpreferences.edit().putString(Email,global_username).commit();
}
public void setuserpwd(String someVariable) {
global_pwd = someVariable;
sharedpreferences.edit().putString(Pwd,global_pwd).commit();
}
}
And here's how to use your new util class
Global global = new Global(context);
global.setusername("foo");
Log.d("TAG", "username from prefs = " + global.getusername());
global.setuserpwd("bar");
Log.d("TAG", "password from prefs = " + global.getusername());
How to save values in preferences (and read) with quite compact way without creating several instances - i want be sure that i have only one instance of pref
A SharedPreferences object is a natural singleton. Your first call to read in a SharedPreferences (e.g., PreferenceManager.getDefaultSharedPreferences()) will create it. Second and subsequent calls within the same process will return that same instance. Hence, so long as you are consistent about where you get your SharedPreferences from, you will only ever have one instance (at most) in your process.
CommonsWare is right, but as i understood, you want somehow to structure you job with Preferences, i use singleton for this, something like this:
public class Preferences {
private static final String USERNAME = "username";
private static final String LOG_TAG = Preferences.class.getSimpleName();
private static Preferences sInstance;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
private Context mContext;
private Preferences(Context context) {
mContext = context;
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mEditor = mSharedPreferences.edit();
}
public static Preferences getInstance(Context context) {
if (sInstance == null) {
sInstance = new Preferences(context);
}
return sInstance;
}
public void setUsername(String username) {
mEditor.putString(USERNAME, username);
commit();
}
public String getUsername() {
return mSharedPreferences.getString(USERNAME, null);
}
public boolean commit() {
return mEditor.commit();
}
}
And where it is necessary write
Preferences.getInstance(this).setUsername("USERNAME");
and read
Preferences.getInstance(this).getUsername();
I want to get a string from my shared preference file and use for more classes, but I don't know why not work.
My reader class is:
import android.app.Activity;
import android.content.SharedPreferences;
public class A {
public static String url2;
public void execute() {
String URLPref = "URL";
SharedPreferences prefs = getSharedPreferences("com.exam.search_preferences",Activity.MODE_PRIVATE);
url2 = prefs.getString(URLPref , "");
}
private SharedPreferences getSharedPreferences(String string,
int modePrivate) {
return null;
}
}
And the second class that uses the string
public class SearchHome extends Activity {
static String url2;
A cls2= new A();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_activity);
cls2.execute();
url2 = A.url2;
Toast.makeText(getApplicationContext(),"URL:" + url2 ,
Toast.LENGTH_LONG).show();
...
Sorry for my bad english, I never learned.But I'm trying!
You need to pass the Context to your class A, because you can get the SharedPreferences from a Context object. NOTE, an Activity is a Context to some extend
public class A {
public static String url2;
/** #param context used to get the SharedPreferences */
public void execute(Context context) {
String URLPref = "URL";
SharedPreferences prefs = context.getSharedPreferences("com.exam.search_preferences",Activity.MODE_PRIVATE);
url2 = prefs.getString(URLPref , "");
}
}
And then pass the Context to your execute method
public class SearchHome extends Activity {
static String url2;
A cls2= new A();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_activity);
// pass context 'this' to the execute function
// This works, because SearchHome extends Activity
cls2.execute(this);
url2 = A.url2;
...
if your data is not confidential it would be a lot easier if you can make a class specially for shared preference and have other activities access it. you will save a lot of time and code will be a lot simpler to follow up
public class HelperShared {
public static final String score = "Score";
public static final String tag_User_Machine = "tag_User_Machine",
tag_Machine_Machine = "tag_Machine_Machine",
tag_Draw_Machine = "tag_Draw_Machine",
tag_Total_Machine = "tag_Total_Machine";
public static SharedPreferences preferences;
public static Editor editor;
public HelperShared(Context context) {
this.preferences = context.getSharedPreferences(score,
Activity.MODE_PRIVATE);
this.editor = preferences.edit();
}
/*
* Getter and Setter methods for Machine
*/
public void setUserMachine(int UserMachine) {
editor.putInt(tag_User_Machine, UserMachine);
editor.commit();
}
public void setMachineMachine(int MachineMachine) {
editor.putInt(tag_Machine_Machine, MachineMachine);
editor.commit();
}
public void setDrawMachine(int DrawMachine) {
editor.putInt(tag_Draw_Machine, DrawMachine);
editor.commit();
}
public void setTotalMachine(int TotalMachine) {
editor.putInt(tag_Total_Machine, TotalMachine);
editor.commit();
}
public int getUserMachine() {
return preferences.getInt(tag_User_Machine, 0);
}
public int getMachineMachine() {
return preferences.getInt(tag_Machine_Machine, 0);
}
public int getDrawMachine() {
return preferences.getInt(tag_Draw_Machine, 0);
}
public int getTotalMachine() {
return preferences.getInt(tag_Total_Machine, 0);
}
}
private SharedPreferences getSharedPreferences(String string,
int modePrivate) {
return null;
}
problem is here.
return null;
you have to return valid SharedPreferences object. otherwise you will always get NullPointerException.
Call this when you want to put a pref:
putPref("myKey", "mystring", getApplicationContext());
Call this when you want to get a pref:
getPref("myKey", getApplicationContext());
You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
Different Modes:
1 MODE_APPEND
This will append the new preferences with the already exisiting preferences
2 MODE_ENABLE_WRITE_AHEAD_LOGGING
Database open flag. When it is set , it would enable write ahead logging by default
3 MODE_MULTI_PROCESS
This method will check for modification of preferences even if the sharedpreference instance has already been loaded
4 MODE_PRIVATE
By setting this mode , the file can only be accessed using calling application
5 MODE_WORLD_READABLE
This mode allow other application to read the preferences
6 MODE_WORLD_WRITEABLE
This mode allow other application to write the preferences
Read More
You just need to make shared prefrences object in class where you want to have data
SharedPreferences prefrences = getSharedPreferences("my prefs",MODE_PRIVATE)
Editor editor = prefrences.edit();
String s = edit.getString("your key",value);
hope it helps !