1

I am making simple login app for my homework project.I wanted to user stay loged in after my app is destroyed.I managed that but now he is always loged in even without entering email and password.I wanted to be able to stay loged in until he press log out button,but even he press it he will go back to log in page but when app is restarted it is again loged in

i tried this: How to keep android applications always be logged in state?

log in page:

public class MainActivity extends AppCompatActivity {

    private EditText email, password;
    private SharedPreferences sharedPreferences;
    public static final String PREF_NAME = "sp_name";
    ConstraintLayout constraintLayout;


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

        email = findViewById(R.id.email_view);
        password = findViewById(R.id.pass_view);
        constraintLayout = findViewById(R.id.activity_main);
        sharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

        //        //uzimam email vrednost iz sharedprefernce
        String storedEmail = sharedPreferences.getString("EMAIL", null);

        //uzimam password vrednosti
       String storedPass = sharedPreferences.getString("PASSWORD", null);

        if(storedEmail != null && storedPass != null){
            // login automatically with username and password
           goToPocetnaStranica();
        }
        Button loginButton = findViewById(R.id.login_button);

        //kada je dugme za login stisnuto loguje se na pocetnu stanu i skladisti login informacije
        //u sharedpreference tak da sledeci put moze da se autologuje bez ponovnog unosa login informacija
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //uzimam email i password
                String getEmail = email.getText().toString();
                String getPass = password.getText().toString();

                //proveravam da li je neko polje prazno
                if (TextUtils.isEmpty(getEmail) || TextUtils.isEmpty(getPass)) {
                    Toast.makeText(MainActivity.this, R.string.obavestenje_za_unos, Toast.LENGTH_SHORT).show();
                } else {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("EMAIL", getEmail);
                    editor.putString("PASSWORD", getPass);
                    editor.apply();
                    email.setText("");
                    password.setText("");
                    goToPocetnaStranica();

                }
            }
        });

    }
    private void goToPocetnaStranica(){
        Toast.makeText(MainActivity.this, R.string.uspesno_logovanje, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this, PocetnaStranica.class);
        startActivity(intent);
    }
}

and my page afer log in:

public class PocetnaStranica extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pocetna_stranica);
        Button logoutButton = findViewById(R.id.logout_button);
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = settings.edit();
                editor.remove("PASSWORD");
                editor.clear();
                editor.apply();
                finish();
            }
        });
    }


}
gojic
  • 363
  • 7
  • 20

2 Answers2

1

@Sebastian makes a good point saying that you need to use the AND operator in the if statement instead of the OR operator. That's because it checks if the email exists or if the password exists. You as you posted only removed the password, so the email is still there. The program takes it as signed in. Also, instead of independently deleting the password and email, just use:

sharedPreferences.clear() //Clears every single value

That would also help with the removal of cache files and save memory space that is wasted.

Edit: The error actually is in your second snippet of code. This code to get the shared preferences:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

This does get the default shared preferences, but instead of getting the preferences where you save your email and your password, it returns a whole different set of shared preferences. What you should do instead is this:

SharedPreferences pref = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

It's the same as you do in MainActivity.java and that was the error: it needed to be the same. Here String PREF_NAME = "sp_name";. The preference name/ids, they exist for this exact thing to identify shared preferences.

If you want to learn more about shared preferences and how they work look at the documentation: https://developer.android.com/reference/android/content/SharedPreferences

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
  • That if statement is in my onclicklistener,and my app is automatically is loged in,without pressing the button..or it's unimportant where it is? – gojic Jul 27 '19 at 14:30
  • It's kind of unimportant. Because when you click the button you check for it and that's where it checks for the values. – Gaurav Mall Jul 27 '19 at 17:21
  • And the button just deletes them. – Gaurav Mall Jul 27 '19 at 17:21
  • NOthing heppened..the problem is in first if statement i mainactivity... but not know what is wrong there – gojic Jul 28 '19 at 00:48
0

It seems like in your main activity you are using the OR ( || ) operator when it should be AND ( && ).

Don’t forget to also delete the email from the preferences.

Sebastian
  • 17
  • 4