Search code examples
androidandroid-sharedpreferences

SharedPreferences using LoginSystem


I have a project with login and user system.I want to my program should work OFFLINE and ONLINE. When online it can be retrieve from database but if offline it must be use sharedpreferences value on device. Anyway, i tried this method but i'm giving every time "NULL" -> (if don't receevie anything return this)

QrcodeID saving to MyQRCodeID string

  editor.putString("MyQRCodeID",QRCodeID); 

I want to here send QRCodeID if available in MyQRCodeID, so login before one time, it must be save in there, else return QRCodeID (first time using or any error)

i.putExtra("QRCodeID",preferences.getString("MyQRCodeID",QRCodeID) );

My scoped code:

 public class CheckLogin extends AsyncTask<String, String, String> {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        String z = "";
        Boolean isSuccess = false;
        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);
        }
        @Override
        protected void onPostExecute(String r) {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(LoginActivity.this, r, Toast.LENGTH_SHORT).show();
            if (isSuccess) {

                Toast.makeText(LoginActivity.this, "Başarıyla Giriş Yapıldı", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                i.putExtra("Name", Name);
                i.putExtra("Surname", Surname);
                i.putExtra("Gsm", Gsm);
                i.putExtra("KategoriID", KategoriID);
                editor.putString("MyQRCodeID",QRCodeID); //QrcodeID saving to MyQRCodeID string i.putExtra("QRCodeID",preferences.getString("MyQRCodeID",QRCodeID) );//I want to here send QRCodeID if avaiable in MyQRCodeID , so login before one time , it must be save in there , else return QRCodeID ( first time using or any error )
                i.putExtra("OnayID", OnayID);
                i.putExtra("Email", Email);
                i.putExtra("UserID", UserID);
                i.putExtra("Password", Password);
            i.putExtra("sharedqr",preferences.getString("MyQRCodeID","NULL")); //I created this for check sharedpreferences working, you can ignore this.
                startActivity(i);
                //finish();
            }
        }

Solution

  • change this

    editor.putString("MyQRCodeID",QRCodeID);
    

    to

    editor.putString("MyQRCodeID",QRCodeID).commit();