是onResume()称之前onActivityResult()?
-
27-09-2019 - |
题
这里是我的应用程序奠定了出来:
- onResume()提示用户登录
- 如果用户登录,他可以继续使用的应用程序 3.如果用户登录在任何时候,我要提示,再次登陆
我如何可以实现这一目标?
这是我的重置:
@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()获取所谓的第一次。
那里是最好的地方提供登录?
其他提示
与碎片它甚至不是一样简单 onActivityResult()
被称为之前的呼叫 onResume()
.如果该活动是回到被安置在临时,你会发现,呼吁以(例如) getActivity()
从 onActivityResult()
将返回null。然而,如果活动没有设置的,一个呼叫 getActivity()
将返回的含的活动。
这种不一致可能是一个来源很难诊断的缺陷但是你可以检查行为的应用程序使开发商选择"不要让活动"。我倾向于保持这个打开-我宁愿看到一个 NullPointerException
在发展中比在生产。
你可能想要考虑抽象离开登录的国家的活动。例如,如果一个用户可以发表意见,让onPost平行动用于登录的国家,并从那里去,而不是从活动的状态。
不隶属于 StackOverflow