31

i am creating login in my app. User type his info i send it to web service to check and get his ID. Now i need to save it somewhere in my app that i could check if he had login.

So where this info should be saved ? I know that every app have it's settings so there should be this info ? Maybe someone could give me more info what and how this is doing.

Thanks.

I found this post: What is the most appropriate way to store user settings in Android application

I believe my question was duplicate. But it would be nice if you have something to share more.

Community
  • 1
  • 1
Streetboy
  • 4,351
  • 12
  • 56
  • 101

4 Answers4

65

First of all save user info like uname & password in SharedPreferences with this code.

SharedPreferences settings = getSharedPreferences("UserInfo", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Username",txtUname.getText().toString());
editor.putString("Password",txtPWD.getText().toString());
editor.commit();

and then get SharedPreferences from below code

SharedPreferences settings = getSharedPreferences("UserInfo", 0);
txtUname.setText(settings.getString("Username", "").toString());
txtPWD.setText(settings.getString("Password", "").toString());
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • 3
    For anyone wondering about compatiility, this is supported in all current API versions (Added in API V1). The SharedPreferences documentation can be found here: http://developer.android.com/reference/android/content/SharedPreferences.html – JakeJ Jan 23 '14 at 15:45
  • 33
    For anyone wondering about security, don't store your user's password in SharedPreferences. – RoraΖ Aug 01 '14 at 12:23
  • 10
    You should NEVER save plain passwords in applications or databases. Instead of saving plain text you can hash it. When you need to validate the given userpasword you can hash it on the same way and compare the hashes. This way is much better than plain text. – CodeNinja Jan 19 '16 at 08:37
  • Can I get it from any Activity inside the App? – GuiDupas Nov 01 '16 at 17:46
  • 1
    @GuiDupas Yes. This is available inside the App. – V.J. Nov 02 '16 at 04:14
30

Answer is Use Application Class or Shared Preferences

I assume you can easily search for the Example of Application Class, SharedPreferences and SQLite Database

So I am mentioning Where Do I Use Which Options

Application Class

When I got anything(var , array , list , objects) to store just only for the LifeTime of the Application..and As it got close (for Android Force Close) I use Application Class.

SharedPreferences

I use this when I got some short information like id,token,username,flags and I want to store that as long as the application rest in the Device..and need to use those information everytime I start the Application..

SQLite Database

When there is a bulk data in Record format (Row - Column) I used it to store in database so it becomes easy to store , retrive and manipulate.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
9

You can use SharedPreferences for this.

Some codes from references (taken from documentation):

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .

       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }

    @Override
    protected void onStop(){
       super.onStop();

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);

      // Commit the edits!
      editor.commit();
    }
}
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
5

if user clear the application data from setting -> application manager -> your application -> clear data

then all data saved in shared preferences will get removed

Rohit Jain
  • 229
  • 1
  • 4
  • 7
  • 3
    Oh really? Wow thanks for pointing that out. I wonder if this holds true for SQLite databases too – MobileMon Jan 03 '14 at 19:14
  • 5
    Yep its confirmed, databases are wiped too. The confirmation dialog tells me all "Files, settings, accounts, databases,etc" will be permanently deleted. Learn something new everyday... – MobileMon Jan 03 '14 at 19:23