Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving user information in app settings

Tags:

android

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.

like image 776
Streetboy Avatar asked Apr 18 '12 12:04

Streetboy


People also ask

How can I persist information in an Android device?

Android supports the following ways of storing data in the local file system: Files - You can create and update files. Preferences - Android allows you to save and retrieve persistent key-value pairs of primitive data type. SQLite database - instances of SQLite databases are also stored on the local file system.


4 Answers

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());
like image 103
V.J. Avatar answered Oct 08 '22 11:10

V.J.


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.

like image 38
MKJParekh Avatar answered Oct 08 '22 11:10

MKJParekh


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();
    }
}
like image 9
ariefbayu Avatar answered Oct 08 '22 11:10

ariefbayu


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

then all data saved in shared preferences will get removed

like image 5
Rohit Jain Avatar answered Oct 08 '22 12:10

Rohit Jain