onResume() 是否在 onActivityResult() 之前调用?
以下是我的应用程序的布局:
- onResume() 提示用户登录
- 如果用户登录,他可以继续使用该应用程序 3.如果用户随时注销,我想提示重新登录
如何实现?
这是我的 MainActivity:
@Override
protected void onResume(){
super.onResume();
isLoggedIn = prefs.getBoolean("isLoggedIn", false);
if(!isLoggedIn){
showLoginActivity();
}
}
这是我的 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);
}
}
}
用户成功登录后:
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( ) 首先被调用。
提示登录的最佳位置在哪里?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,对 onActivityResult 的调用发生在 onResume 之前(请参阅 文档)。您确定确实使用
startActivityForResult
启动了所需的 Activity,并且在向 Activity 返回值之前将调用的 Activity 的结果设置为RESULT_OK
吗?尝试在onActivityResult
中添加一个Log
语句来记录该值并确保它被命中。另外,您在哪里设置 isLoggedIn 首选项的值?看起来您应该在登录活动返回之前将其设置为true
,但这显然没有发生。编辑
文档说:
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 toRESULT_OK
before returning a value to your activity? Try just putting aLog
statement in youronActivityResult
to log that value and make sure that gets hit. Also, where are you setting the value of theisLoggedIn
preference? It seems like you should be setting that totrue
in your login activity before it returns anyways, but that's clearly not happening.Edit
The docs say:
对于片段,它甚至不像在调用
onResume()
之前调用onActivityResult()
那么简单。如果您要返回的 Activity 在此期间被释放,您会发现从onActivityResult()
调用(例如)getActivity()
将返回 null。但是,如果该 Activity 尚未被释放,则调用getActivity()
将返回包含的 Activity。这种不一致可能是难以诊断的缺陷的根源,但您可以通过启用开发人员选项“不保留活动”来检查应用程序的行为。我倾向于将其保持打开状态 - 我宁愿在开发中而不是在生产中看到 NullPointerException。
With fragments it isn't even as simple as
onActivityResult()
being called before the call toonResume()
. If the activity that you are returning to was disposed of in the interim, you will find that calls to (for example)getActivity()
fromonActivityResult()
will return null. However, if the activity has not been disposed of, a call togetActivity()
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.您可能需要考虑从活动中抽象出登录状态。例如,如果用户可以发表评论,请让 onPost 操作 ping 登录状态并从那里开始,而不是从活动状态开始。
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.
像
onResume
这样的回调方法不适合实现所请求的功能。我建议开设一堂课&在那里添加登录/注销功能。
当收到注销回调时,调用登录功能。
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.