在 onActivityResult()之前调用 onResume()吗?

下面是我的应用程序的布局:

  1. OnResume ()用户被提示登录
  2. 如果用户登录,他可以继续使用应用程序 3. 如果用户在任何时候注销,我想再次提示登录

我怎么才能做到呢?

以下是我的主要活动:

@Override
protected void onResume(){
super.onResume();


isLoggedIn = prefs.getBoolean("isLoggedIn", false);


if(!isLoggedIn){
showLoginActivity();
}
}

以下是我的登录活动:

@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);
}
}
}

用户成功登录后:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "BOOM SHAKA LAKA!",Toast.LENGTH_SHORT).show();
}
}

问题是,onResume ()是在 onActivityResult ()之前调用的,所以当用户成功登录时,我的主要活动不会得到通知,因为 onResume ()会先被调用。

哪里是提示登录的最佳地点?

52999 次浏览

The call to onActivityResult happens before onResume, actually (see the docs). Are you sure you're actually starting the activity you wanted with startActivityForResult and that you're setting the result of the invoked activity to RESULT_OK before returning a value to your activity? Try just putting a Log statement in your onActivityResult to log that value and make sure that gets hit. Also, where are you setting the value of the isLoggedIn preference? It seems like you should be setting that to true in your login activity before it returns anyways, but that's clearly not happening.

Edit

The docs say:

You will receive this call immediately before onResume() when your activity is re-starting.

You may want to consider abstracting away the login state from the activity. For example if a user can post comments, let the onPost action ping for login state and go from there, instead of from the activity state.

With fragments it isn't even as simple as onActivityResult() being called before the call to onResume(). If the activity that you are returning to was disposed of in the interim, you will find that calls to (for example) getActivity() from onActivityResult() will return null. However, if the activity has not been disposed of, a call to getActivity() will return the containing activity.

This inconsistency can be a source of hard-to-diagnose defects but you can check the behaviour of your application by enabling the developer option "Don't keep activities". I tend to keep this turned on - I'd rather see a NullPointerException in development than in production.

Callback methods like onResume are not suitable places to achieve the requested functionality. I would suggest making a class & adding the sign-in/sign-out functionality there. when a signout callback is received, then call the sign-in functionality.