I have a login page which requires user to enter their name. On the next page, I'm trying to show "Hi, (name)". How can I do that? And can I store this permanently like, when the user restarts the app, it does not require them to key in their name anymore?
Asked
Active
Viewed 66 times
0
-
possible duplicate of [android save user preferences permanently?](http://stackoverflow.com/questions/7920151/android-save-user-preferences-permanently) – MrEngineer13 Jul 26 '14 at 13:31
-
but how do i implement it? im confused – user3774763 Jul 26 '14 at 13:42
-
and i dont think user's name is a preferences – user3774763 Jul 26 '14 at 13:48
-
I disagree, it's what they PREFER to be known as – MrEngineer13 Jul 26 '14 at 13:50
-
so i can use saveAsPreference in OnStop() and retrievePreference in OnStart()? – user3774763 Jul 26 '14 at 13:55
-
1Sure, or save it when you get it and retrieve it when you need it – MrEngineer13 Jul 26 '14 at 13:57
-
alright, thanks.. how do i unflag a comment? i accidentally clicked on it, sorry! – user3774763 Jul 26 '14 at 14:10
2 Answers
1
You need to get the name from the text view:
String name = mTextview.getText();
When you launch the next activity you can say:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("NAME", name);
startActivity(intent);
In next Activity you can get it using:
Bundle extra = getIntent().getExtras();
String name = extra.getStringExtra("NAME");
Another approach, if you want to store it permanently, you can use SharedPreferences.

AlexBalo
- 1,258
- 1
- 11
- 16
-
If it solves your problem sign it as response :D it really helps. Thx – AlexBalo Jul 26 '14 at 14:27
-
1
You can use SharedPreference, like they said.
example :
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("uName", txtUname.getText());
editor.commit();
When required, you can use 'getString("uName")' in place of putString() to read the value.
A stack-overflow link that might help you in-case of any confusions.
Android - Storing/retrieving strings with shared preferences

Community
- 1
- 1

Priyabrata
- 1,202
- 3
- 19
- 57
-
-
do i still need the login.xml to be a launcher since it's permanent? – user3774763 Jul 26 '14 at 14:31
-
It really doesn't depend on who is launcher. You can save it anywhere and use it anywhere you want. – Priyabrata Jul 26 '14 at 14:33