How to create multiple users registration and login activity in an android?

578 views Asked by At

I've made registration and login activity in an android using shared preferences. User is able to registered and logged in successfully. But my problem is when new user is registered, it delete the previous user's registration details.

Here is my registration page:

 public class Registration extends Activity implements 
  View.OnClickListener 
  {
  SharedPreferences pref;
  Editor editor;
  TextView btn_registration , btn_formlogin;
  EditText et_name, et_pass, et_email , et_phone ;
  ImageView btn_upload ;
  private static final int PICK_IMAGE_REQUEST = 0;
  private Uri mImageUri;

  SessionManager session;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);


    et_name = (EditText) findViewById(R.id.edt_Name);
    et_pass = (EditText) findViewById(R.id.edt_userPassword);
    et_email = (EditText) findViewById(R.id.edt_userEmail);
    et_phone = (EditText) findViewById(R.id.edt_Phone);
    btn_registration = (TextView) findViewById(R.id.btn_registration);
    btn_formlogin = (TextView) findViewById(R.id.btn_formlogin);
    btn_upload = (ImageView)findViewById(R.id.img_upload);

    Map<String,String> values = new HashMap<String,String>();
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    btn_upload.setOnClickListener(this);
    SharedPreferences preference = 
    PreferenceManager.getDefaultSharedPreferences(this);
    final String mImageUri = preference.getString("image", null);
    btn_upload.setImageResource(R.drawable.adduser);

    // creating an shared Preference file for the information to be stored
    // first argument is the name of file and second is the mode, 0 is 
    private mode

    pref = getSharedPreferences("USER_CREDENTIALS", 0);
    // get editor to edit in file
    editor = pref.edit();


    // the tap of button we will fetch the information from four edittext
     btn_registration.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick (View v) {


            boolean validName = true;
            boolean validPass = true;
            boolean validPhone = true;
            boolean validEmail = true;

            String name = et_name.getText().toString();
            String email = et_email.getText().toString();
            String pass = et_pass.getText().toString();
            String phone = et_phone.getText().toString();


            if (isEmpty(et_name)) {
                et_name.setError("Enter the Name");
                validName = false;

            }


            if (isEmpty(et_pass)) {
                et_pass.setError("Password can't be null");
                validPass = false;

            }

            if (isEmail(et_email) == false) {
                et_email.setError("Enter valid email");
                validEmail = false;

            }

            if (isValidMobile(et_phone) == false) {
                validPhone = false;

            }


            final String required_email= pref.getString("Email",null);
            final String required_phone=pref.getString("Phone",null);

            if(et_email.equals(required_email)){
                validEmail = true;
            }
            else if(et_phone.equals(required_phone)){
                validPhone = true;

            }


            if (validName && validPass && validPhone && validEmail){

                // as now we have information in string. Lets stored them 
                with the help of editor
                editor.putString("Name" , name );
                editor.putString("Email", email);
                editor.putString("Password", pass);
                editor.putString("Phone", phone);
                editor.putString("image", String.valueOf(mImageUri));

                editor.commit();   // commit the values

                Toast.makeText(Registration.this, "Registration 
                Successful", Toast.LENGTH_LONG).show();

                session = new SessionManager(getApplicationContext());

                session.createLoginSession(name, email, mImageUri);

                // after saving the value open next activity
                Intent ob = new Intent(Registration.this, 
                DashBoard.class);
                startActivity(ob);

            }

             else{
                Toast.makeText(Registration.this, "Registration 
              unsuccessful! Please retry.", Toast.LENGTH_LONG).show();

             }
           }
          });

I just want to know how to register multiple user using shared preferences file ?

2

There are 2 answers

2
aria mossar On

If you will have multiple users, it is better to use a database(sqlite) than a shared preferences.

5
Sandeep Malik On

in shared preference you can save only single value in a object if you want to save multiple users in your device then you can use room database to store multiple user details in your device. Shared Preference can be used only for single information or if you want to store multiple values in shared preference then you can store arrayList in shared prefefrence.

save array list in shared preference:-

 private static SharedPreferences mPrefs;
 private static SharedPreferences.Editor mPrefsEditor;

 public static Set<String> getCamEval(Context ctx) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        return mPrefs.getStringSet("sdk_camEval", null);
    }

 public static void setCamEval(Context ctx, ArrayList<String> value) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        mPrefsEditor = mPrefs.edit();
        Set<String> set = new HashSet<>();
        set.addAll(value);
        mPrefsEditor.putStringSet("sdk_camEval", set);
        mPrefsEditor.commit();
    }