Get Android shared preferences value in activity/normal class
-
I have made a shared preference activity that store the user settings, now i want to get values in a activity or normal java class.please provide a solution or example i have already tried this code but failed. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences channel=this.getSharedPreferences(strFile, Context.MODE_PRIVATE); strChannel=channel.getString(keyChannel,"Default").toString(); Toast.makeText(getApplicationContext(), strChannel, Toast.LENGTH_LONG).show(); } in this code strfile for eg. com.android.pack.ClassName is SharedPreference Activity from values to be fetched, and keyChannel is key that is same in SharedPreference Activity. Kindly provide the solution.
-
Answer:
If you have a SharedPreferenceActivity by which you have saved your values SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String imgSett = prefs.getString(keyChannel, ""); if the value is saved in a SharedPreference in an Activity then this is the correct way to saving it. SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); SharedPreferences.Editor editor = shared.edit(); editor.putString(keyChannel, email); editor.commit();// commit is important here. and this is how you can retrieve the values. SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); String channel = (shared.getString(keyChannel, "")); Also be aware that you can do so in a non-Activity class too but the only condition is that you need to pass the context of the Activity. use this context in to get the SharedPreferences. mContext.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
DeepSan at Stack Overflow Visit the source
Related Q & A:
- How to open current activity with android notification?Best solution by Stack Overflow
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
- How to send data from Fragment to an Activity in Android?Best solution by forum.xda-developers.com
- What is the best camcorder to get for a college class?Best solution by Yahoo! Answers
- Is it better to get a higher grade in a easier class or a mediocre/low grade in a hard class?Best solution by greatcollegeadvice.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.