Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

SharedPreferences

/*============StartActivity.java============*/

public class StartActivity extends AppCompatActivity {

private static final String PREF_LOGIN = "LOGIN_PREF";
private static final String KEY_CREDENTIALS = "LOGIN_CREDENTIALS";


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences preferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);

    Intent intent = null;
    if(preferences.contains(KEY_CREDENTIALS)){              //if user is currently logged in;
        intent = new Intent(this, MainActivity.class);
    }else {                                                 //if user is not yet logged in;
        intent = new Intent(this, LoginActivity.class);
    }
    startActivity(intent);
}

}

/*============LoginActivity.java============*/

public class LoginActivity extends AppCompatActivity {

public static final String PREF_LOGIN = "LOGIN_PREF";
public static final String KEY_CREDENTIALS = "LOGIN_CREDENTIALS";

private Button loginButton;

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

    //...

    loginButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //call login api...


            //on your sucess callback; we save the credentials...
            /*
            SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
            editor.putString(KEY_CREDENTIALS, "DUMMY CREDENTIALS");
            editor.commit();
            */

            //on your failure callback; we clear the credentials...

            /*
            SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();
            */
        }


    });
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to compare doubles in java 
Java :: is c# similar to java 
Java :: java loop through array 
Java :: java string loop backwards 
Java :: reverse a doubly linked list 
Java :: multiple spinner android 
Java :: how to create a node in java 
Java :: How to change integers into float in java 
Java :: how to find a word in a statement java 
Java :: string array to string stream java 
Java :: string vs new string 
Java :: java scanner next() 
Java :: MD5 java 
Java :: json java 
Java :: get value from Spring application.properties 
Java :: streams in java 
Java :: The Longest Substring 
Java :: islowercase java 
Java :: android studio convert java to kotlin 
Java :: new int[] java 
Java :: Java Iterating through LinkedList 
Java :: matrix dimensions 
Java :: java array 
Java :: junit dependency 
Java :: Is the main method compulsory in Java? 
Java :: madhava kripa 
Java :: java import keyword 
Java :: access char in string 
Java :: AccountDriver.java 
Java :: layout focus from recycleview not from top in fragment inside nestedScrollview in android xml 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =