Streetboy
Streetboy

Reputation: 4401

Saving user information in app settings

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.

Upvotes: 31

Views: 46024

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 229

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

then all data saved in shared preferences will get removed

Upvotes: 5

MKJParekh
MKJParekh

Reputation: 34301

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.

Upvotes: 30

V.J.
V.J.

Reputation: 9580

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());

Upvotes: 66

ariefbayu
ariefbayu

Reputation: 21979

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();
    }
}

Upvotes: 9

Related Questions