Así es como se presenta mi aplicación:¿onResume() llamado before onActivityResult()?
- onResume() se pide al usuario iniciar sesión
- Si usuario inicia sesión, puede seguir utilizando la aplicación 3. Si el usuario cierra la sesión en cualquier tiempo, quiero solicitar nuevamente el inicio de sesión
¿Cómo puedo lograr esto?
Aquí es mi MainActivity:
@Override
protected void onResume(){
super.onResume();
isLoggedIn = prefs.getBoolean("isLoggedIn", false);
if(!isLoggedIn){
showLoginActivity();
}
}
Aquí es mi LoginActivity:
@Override
protected void onPostExecute(JSONObject json) {
String authorized = "200";
String unauthorized = "401";
String notfound = "404";
String status = new String();
try {
// Get the messages array
JSONObject response = json.getJSONObject("response");
status = response.getString("status");
if(status.equals(authorized)){
Toast.makeText(getApplicationContext(), "You have been logged into the app!",Toast.LENGTH_SHORT).show();
prefs.edit().putBoolean("isLoggedIn",true);
setResult(RESULT_OK, getIntent());
finish();
}
else if (status.equals(unauthorized)){
Toast.makeText(getApplicationContext(), "The username and password you provided are incorrect!",Toast.LENGTH_SHORT).show();
prefs.edit().putBoolean("isLoggedIn",true);
}
else if(status.equals(notfound)){
Toast.makeText(getApplicationContext(), "Not found",Toast.LENGTH_SHORT).show();
prefs.edit().putBoolean("isLoggedIn",true);
}
} catch (JSONException e) {
System.out.println(e);
} catch (NullPointerException e) {
System.out.println(e);
}
}
}
Después de que el usuario ha iniciado la sesión correctamente:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "BOOM SHAKA LAKA!",Toast.LENGTH_SHORT).show();
}
}
El problema es, onResume() se llama beforeActivityResult() así que cuando el usuario ha iniciado sesión correctamente, mi actividad principal no se noti fied porque onResume() se llama primero.
¿Dónde está el mejor lugar para solicitar el inicio de sesión?
estoy configurando isLoggedIn después de que el usuario haya iniciado sesión. Vea mi código actualizado. no estoy seguro de qué está mal? –
tiene razón, onActivityResult() se está llamando antes onResume(). ¿No estoy seguro de por qué mis preferencias se están leyendo mal entonces? –
agregó una confirmación() después de mi putBoolean. Hizo el truco. –