0

hi, i am trying save information user after registration and when time app run , must be sent to main page but i do not want show Login Page ...

enter image description here

in class Register Activity : note : this code is from RegisterActivity

// for save status user
public void setSession(){
    // made model of sharePrefrence
   SharedPreferences  sp = getSharedPreferences(Key_SessionUsers,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putBoolean(Key_Status,true);
    editor.commit();

}

in class Register LoginActivity : note : "setSession()" from registerActivity that call in class LoginActivity .

try {
    RegisterActivity registerActivity = new RegisterActivity();
    if (registerActivity.sp.getBoolean(registerActivity.Key_Status,false) == true){
        startActivity(new Intent(LoginActivity.this , MainActivity.class));
    }

}catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}

Error : nullPointerException .

Amin
  • 342
  • 4
  • 15

3 Answers3

1

this method for save value in sharedPreferences and be worked good

public void setSesstionUser(){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(RegisterActivity.this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean("Status_user", true);
    editor.apply();

// user is login then Status_user=>true

}

this method can in put called any class .for example, i have Registration Activity and i wanna storing session to user after than registre user's information in server.. and still save in defualt sharePrefrence.. so when time storing user information to server , we should use method '' setSesstionUser()' into storing sharePrefrence

so finally be called in class that ...

   // check_state_user --------------------------------------------------------
    try {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
        boolean memberStatus = preferences.getBoolean("Status_user", false);
        if(memberStatus){ startActivity(new Intent(this,MainActivity.class)); finish();}

    }catch (Exception e){
       // Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
    }

this method help to us if for login user in app is ok == > so going to main page

picture 1: enter image description here

2: enter image description here

Community
  • 1
  • 1
Amin
  • 342
  • 4
  • 15
0

I think it should be:

try {
    RegisterActivity registerActivity = new RegisterActivity();
    if (getSharedPreferences().getBoolean(registerActivity.Key_Status,false) == true){
        startActivity(new Intent(LoginActivity.this , MainActivity.class));
    }

}catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}

because sp is not a public variable. The new part is getSharedPreferences() instead of registerActivity.sp.

Axiumin_
  • 2,107
  • 2
  • 15
  • 24
  • why dont save are you have good way for save session user after than registration -Axium – Amin Jan 07 '19 at 00:39
  • @oktay Because that's not how `SharedPreferences` work. – Axiumin_ Jan 07 '19 at 00:39
  • how to work shared Preference with Foreign class . are you know good way? – Amin Jan 07 '19 at 00:43
  • Foreign Class? As in? – Axiumin_ Jan 07 '19 at 00:44
  • you say using a Foreign Class is better?but i dont ues foregin class .are you good way for using sharedPreferences in foreign class – Amin Jan 07 '19 at 00:58
  • this isn't a foreign class. this is just getting a new version of a `SharedPreferences` object because the way you're doing it is not how it works. – Axiumin_ Jan 07 '19 at 01:01
  • Also, `SharedPreferences sp = getSharedPreferences(Key_SessionUsers,Context.MODE_PRIVATE);` equates to a new local variable, not your global `sp` variable, causing it to be null. – Axiumin_ Jan 07 '19 at 01:03
  • i find good way for save session . i use default Shared Preference and that problem is fix. – Amin Jan 07 '19 at 14:04
  • 'SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("Status_user", flase); editor.apply(); ' **this method for save value in sharedPreferences and be worked good** – Amin Jan 07 '19 at 14:05
0

That is not how to retrieve data with SharedPreferences. You can get your boolean this way:

//Create and initialize your sharedpreferences object
SharedPreferences sp = getSharedPreferences(Key_SessionUsers,Context.MODE_PRIVATE);

//get your boolean from the sharedpreferences object
boolean val = getSharedPreferences.getBoolean(registerActivity.Key_Status,false);

Since you have your boolean right after this, you can go ahead and do your comparisons.

I hope this helps.. Merry coding!!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
  • you know why send null pointer for me , because made sharedPreferences in Registration Activity that should save true and calling in loginActivity .. but i dont find a good way – Amin Jan 07 '19 at 00:53