简体   繁体   中英

How to Implement Only one time login activity open after splash screen next time it will open direct main activity 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) {
    }

}

First my splash Activity Launch and then Login Activity Open. After Login my Profile Activity Open. I Want open Profile Activity Directly if user login one time and key/value stored. Above Code for Login activity to use SharedPreferences . and am getting key/value form Response. Please Tell me how to implement only one time login activity.

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

This is my key/value get in Profile Activity

-----1st run-----

Splash Activity

check if there is value in shared preferences There wont be any value as you have not yet logged in

Show Login activity

Login Activity

Ask user to enter credentials Save credentials / customer ID in shared preferences

Show Profile activity

-----2nd and further runs-----

Splash Activity

check if there is value in shared preferences now you will have the customer ID in SP if you find ID

Show Profile activity

if you dont find ID (in case user clears app data)

Show Login activity

Give your SharedPreferences file a name and save it to a static variable

public static final String PREFS_NAME = "MyLoginPrefsFile";

When user has successfully logged in, save this information We need an Editor object to make preference changes.

Add this code where you login successfully.

 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, check 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
}

Have an activity, let us say launcher activity, which reads shared preferences and checks if valid customer id is found. if found then moved to profile activity otherwise goto login activity.

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mAppContext);
String custId = prefs.getString("cust_id", null);
if (custId) {
            Intent intent = new Intent(LauncherActivity.this,ProfileActivity.class);

} else {
            Intent intent = new Intent(LauncherActivity.this,LoginActivity.class);

}
in your login activity add this line


 public static final String MyPREFERENCES1 = "somthing" ;
 public static final String Status1= "password";
 SharedPreferences sharedpreferences;
  sharedpreferences =   
 getActivity().getSharedPreferences(MyPREFERENCES1, 
 getActivity().MODE_PRIVATE);

     String satus = userpasswd.getText().toString();//password or user name
            SharedPreferences.Editor editor = 
            sharedpreferences.edit();

            editor.putString(Status1, satus);

            editor.commit();

 in your splash screen activity

 public static final String MyPREFERENCES1 = "somthing" ; 

 SharedPreferences sharedpreferences;
 private static int SPLASH_TIME_OUT = 3000;
 sharedpreferences = getSharedPreferences(MyPREFERENCES,
 Context.MODE_PRIVATE);
String value=sharedpreferences.getString("password", "");

if(value.equalsIgnoreCase(""))
    {
      new Handler().postDelayed(new Runnable() {
       @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent intent =new Intent(SplashScreenActivity.this, 
              LoginActivity.class);


                startActivity(intent);

            }
        }, SPLASH_TIME_OUT)
       }
    else
    {

        new Handler().postDelayed(new Runnable() {
    @Override
            public void run() {

              Intent intent =new Intent(SplashScreenActivity.this, 
              HomeActivity.class);


                startActivity(intent);
             }
        }, SPLASH_TIME_OUT);

hope this will help you....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM