Java – How to open the login activity only once after the splash screen, and the next time it will open the main activity directly in android

How to open the login activity only once after the splash screen, and the next time it will open the main activity directly in android… here is a solution to the problem.

How to open the login activity only once after the splash screen, and the next time it will open the main activity directly in android

private void handleResponse(JSONObject serverResponse) {
    int success = 0;
    try {
        success = serverResponse.getInt(Responce.TAG_SUCCESS);
        if (success == 1) {
            progressDialog.dismiss();
            after login we want to store user's id into shared preferences
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("cust_id", serverResponse.getString("CustomerId"));
            editor.commit();
            Toast.makeText(LoginActivity.this, serverResponse.getString((Responce.TAG_MESSAGE)), Toast.LENGTH_LONG).show();
            Intent intent = new Intent(LoginActivity.this,ProfileActivity.class);
            startActivity(intent);
        } else {
            progressDialog.dismiss();
            Toast.makeText(LoginActivity.this, serverResponse.getString(Responce.TAG_MESSAGE), Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e) {
    }

}

Start the Startup activity first, and then open the Login activity.
After logging in, My Profile Activity opens.
If the user logs in once and stores the key/value, I want to open the profile activity directly.
The login activity code above uses SharedPreferences. And get the key/value from the response.
Please tell me how to perform the login activity only once.

final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
string value = (mSharedPreferences.getString("cust_id", "Default_value"));

This is the key/value I got in the profile activity

Solution

Name your SharedPreferences file and save it to a static variable

public static final String PREFS_NAME = "MyLoginPrefsFile";

This information is saved after the user logs on successfully
We need an editor object to change preferences.

Add this code where you successfully logged in.

 SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0);  0 - for private mode
    SharedPreferences.Editor editor = settings.edit();

Set "hasLoggedIn" to true
    editor.putBoolean("hasLoggedIn", true);

 Commit the edits!
    editor.commit();

In your splash screen, select this

SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0);
Get "hasLoggedIn" value. If the value doesn't exist yet false is returned
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);

if(hasLoggedIn)
{
    Go directly to main activity.
}
else
{
   Show Login Activity
}

Related Problems and Solutions