Q:

Android Shared Preference how to register user then keep logged on

I'm fairly new to Android Studio and I'm working with the code below to try and make it so that instead of always prompting the user to log in, it should save/register the user on the first go so they are always logged in when the application runs, i.e. skip the login. 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // declaring variebles
    etUsername = (EditText)findViewById(R.id.etUsername);
    etPassword= (EditText)findViewById(R.id.etPassword);
    btnLogin = (Button)findViewById(R.id.btnLogin);
    etIpAddress = (EditText) findViewById(R.id.etIpAddress);

    // setting up things for login button
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String ipAddress = etIpAddress.getText().toString();

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);

            sharedPreferences.edit()
                    .putString("ip", ipAddress)
                    .apply();

            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            performLogin(username, password, ipAddress);
        }
    });
}

A:

Just check in onCreate if previous ip value exist
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
    if (sharedPreferences.contains("ip")) {                 
         performLogin(username, password, sharedPreferences.getString("ip","default value here"));
    }

    //other code 
}

