0

I'm making the android app and i use the registration, login and logout screen. What I need is to save the login state. If the user logins once then he will never login again until his logout. I use SharedPareference but I face the many problems.

There is my code:

public class LoginPage extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    Button login, register, database;
    EditText username;
    EditText password;
    TextView msg;
    String db_username;
    MyDBManager db;

    static final int REGISTRATION_REQUEST = 1;  // The request code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginpage);
        db = new MyDBManager(this);

        msg = (TextView) findViewById(R.id.msg);
        login = (Button) findViewById(R.id.button);
        register = (Button) findViewById(R.id.button2);
        database = (Button) findViewById(R.id.button3);
        username = (EditText) findViewById(R.id.user);
        password = (EditText) findViewById(R.id.pass);


        // Click and move to the next activity
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    db.open();
                    Cursor c = db.getUser(username.getText().toString(), password.getText().toString());
                    if (c.moveToFirst()) {
                        db.close();
                        Intent intent = new Intent(LoginPage.this, Menu.class);
                        intent.putExtra("Username", username.getText().toString());
                        startActivity(intent); // call Login Activity
                }
                else
                {
                    db.close();

                    // set error message when the username and/or password is not valid
                    msg.setText("Unable to login: wrong username or password!");
                    // Stay at the current activity.
                }
                 {

                }




                }
            });





        // Click and move to the next activity
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginPage.this, Registration.class);
                startActivityForResult(intent, REGISTRATION_REQUEST);
            }
        });

        // Click and move to the next activity
        database.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginPage.this, Database.class);
                startActivity(intent);
            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REGISTRATION_REQUEST) {
            //if(resultCode == Registration.RESULT_OK){
            Log.d("Pikatchu", "User successfully registered!");
            Bundle res = data.getExtras();
            String result = res.getString("result");
            System.out.println(result);
            msg.setText(result);

        }
    }


}

Can you please give some advices why does my code doesn't work?

  • Step1: Upon login success for first time store those username, password in shared preferences or database. Step 2: Next time when user coming to login page look up in your cache (from database or shared preferences) whether user details exist in the cache or not in Activity onCreate(). If exists take hime directly to Menu Page or Home page, else do nothing I mean let user fills user name and password himself. usually when login success , will store the corresponding userId in the cache not the user name and password. – Chaitu Jan 28 '20 at 04:44
  • I implement this method but failed the error are occurs. – Hassan Ali Mughal Jan 28 '20 at 05:40
  • kindly give me the suitable solution – Hassan Ali Mughal Jan 28 '20 at 06:28

1 Answers1

0

when user logins store boolean loggedin into sharedpreference, and during launch check whether user loggedin, then you can navigate to necessary route, boolean loggedin default should be false

BosS
  • 447
  • 2
  • 9