Para lograr esto con SharedPreferences
que podría hacer algo como esto:
insertar los siguientes en cualquier Clase se ve más en forma. Supongamos que inserta esto en la clase Example
.
//Give your SharedPreferences file a name and save it to a static variable
public static final String PREFS_NAME = "MyPrefsFile";
Ahora, en el método que evalúa si el usuario inicia sesión con éxito en, haga lo siguiente. Observe la clase Example
, debe cambiar esto para que coincida con su código.
//User has successfully logged in, save this information
// We need an Editor object to make preference changes.
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();
Por último, cuando su aplicación se inicia ahora se puede evaluar si el usuario ya ha iniciado sesión o no. Sin embargo, observe la clase Example
que debe cambiar.
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.
}
Esperanza esto ayuda
EDIT: Para evitar que el usuario utilice el botón Atrás para volver a la actividad de sesión que tiene que finish()
la actividad después de comenzar una nueva.
siguiente código tomado de Forwarding.java | Android developers
// Here we start the next activity, and then call finish()
// so that our own will stop running and be removed from the
// history stack
Intent intent = new Intent();
intent.setClass(Forwarding.this, ForwardTarget.class);
startActivity(intent);
Example.this.finish();
Por lo tanto, lo que tiene que hacer en su código es llamar a la función finish()
en la actividad de inicio de sesión, después de llamar startActivity()
.
Consulte también: Removing an activity from the history stack
El enlace que intentó compartir con nosotros estaba mal formado. Por favor edite su pregunta para agregar el enlace correcto. –