I want the Android java code for saving the data using shared preferences and retrieving it when ever wanted. This interaction should be between two java classes(two activities).
Asked
Active
Viewed 92 times
-5
-
2This *not* a "do the work for me and fast cause its urgent" forum. Have you read this https://stackoverflow.com/help/how-to-ask? – Yannis Jan 29 '18 at 11:53
-
**1** - We're not going to work for you for free (this is not a gimme-teh-codez site). **2** - What's urgent *for you* is only urgent *for you*. **3** - Improve your googling skills. – Phantômaxx Jan 29 '18 at 12:41
-
I'm extremely sorry. I didn't know actually. Please excuse me. I apologize for that. #Kling Klang – user9283483 Jan 29 '18 at 12:49
2 Answers
0
SharedPreference sp = getSharedPreference("android",Context.Mode_Private);
for saving data in shared preferences:
sp.edit.putString("text","Hello").apply();
for retrieving anywhere in the code:
String text = sp.getString("text",null);

gmc
- 3,910
- 2
- 31
- 44

Rishabh Gupta
- 85
- 1
- 1
- 7
-
By answering this kind of posts, you encourage more lazy users to post them. – Phantômaxx Jan 29 '18 at 12:42
-
@user9283483 You have to show what you tried (meaning: your code), if you want some help. That's the rule. – Phantômaxx Jan 29 '18 at 21:50
-
While creating an account in Stack Overflow there didn't show any rule or terms and conditions to follow. Then how can a new user get to know the rules being a beginner? And what ever it may be, you can't tell me lazy and you have no rights to justify an user in such a way. It's not a correct manner. If you are an employee in this stack overflow then set a term and conditions part during user sign up. – user9283483 Jan 30 '18 at 02:57
-
1@user9283483 You're called lazy becuase 1. You haven't shown any effort on your part to show that you've done any work, done any code 2. you haven't shown research effort because if you did, you'd see that there are literally hundreds of simple examples on how to use shared preferences both inside stack overflow, and outside on the web. Also note that nobody here is obligated to help you, we're all just helping each other out, so be a little more polite. Also it's you're duty to figure out the rules on any forum before you post. That's basic etiquette – ColonD Jan 30 '18 at 05:14
-
@user9283483 Moreover, you should take the [tour](https://stackoverflow.com/tour). And follow the [how to ask](https://stackoverflow.com/help/how-to-ask) paragraph of the help section. – Phantômaxx Jan 30 '18 at 08:27
-
These things could be said after my mistake but using words like lazy is not correct. Be polite. – user9283483 Jan 30 '18 at 09:36
0
Save Data on Login Screen using Shared Preferences: LoginActivity.java
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = (this.getApplicationContext()).getSharedPreferences("LOGIN_CREDENTIAL",
MODE_PRIVATE);
//SharedPreferences
editor = pref.edit();
editor.putBoolean("login_status", true);
editor.putString("USER_NAME", "" + uName);
editor.putString("PASSWORD", "" + pwd);
editor.putString("USER_ID", "" + mList.get(0).getUserData().get(0).getId());
editor.putString("ROLE_ID", "" +mList.get(0).getUserData().get(0).getRoleId());
editor.putString("DC_ID", "" + mList.get(0).getUserData().get(0).getDcId());
editor.putString("STORE_NAME", "" + mList.get(0).getUserData().get(0).getName());
editor.commit();
Get Data on Next Any Activity :
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = (this).getSharedPreferences("LOGIN_CREDENTIAL",MODE_PRIVATE);
storeId = pref.getString("STORE_ID", "");
userId = pref.getString("USER_ID", "");
dcId = pref.getString("DC_ID", "");
userName = pref.getString("USER_NAME", "");

Abhishek kumar
- 4,347
- 8
- 29
- 44
-
1By answering this kind of posts, you encourage more lazy users to post them. – Phantômaxx Jan 29 '18 at 12:42
-
Thanks for the information , next time I will take care these things. @KlingKlang – Abhishek kumar Jan 29 '18 at 12:47