Skip to content

Instantly share code, notes, and snippets.

@jmsalcido
Created July 28, 2016 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmsalcido/23b0580b7eecc2bf1b7dd36273326d7b to your computer and use it in GitHub Desktop.
Save jmsalcido/23b0580b7eecc2bf1b7dd36273326d7b to your computer and use it in GitHub Desktop.
desarrolladores-android-pregunta-login
public class SharedPreferencesRepository {
private final Context context;
public SharedPreferencesRepository(Context context) {
this.context = context;
}
public boolean saveString(String key, String source) {
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putString(key, source);
return editor.commit();
}
public String getStringValue(String key) {
SharedPreferences sharedPreferences = getSharedPreferences();
return sharedPreferences.getString(key, "");
}
private SharedPreferences getSharedPreferences() {
return context.getSharedPreferences("tuapp_preferences_o_como_quieras", Context.MODE_PRIVATE);
}
}
public class TuLoginActivity extends Activity {
private SharedPreferencesRepository sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = new SharedPreferencesRepository(this);
String token = sharedPreferences.getStringValue("user_token");
if (token == null || token.isEmpty()) {
// muestras la UI de login
setContentView(R.layout.login_activity);
} else {
// aqui metes todo lo relacionado ó redireccionas a la actividad/fragmento/whatever que estes loggeado
startActivity(new Intent(this, LoggedInActivity.class);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment