java中的onresume_java – onActivityResult()之前调用onResume()?

java中的onresume_java – onActivityResult()之前调用onResume()?这里是我的应用程序布局:>onResume()提示用户登录>如果用户登录,他可以继续使用该应用程序3.如果用户在任何时候退出,我想再次提示登录我该如何实现呢?这里是我的MainActivity:@OverrideprotectedvoidonResume(){super.onResume();isLoggedIn=prefs.getBoolean(“isLoggedIn”,…

大家好,又见面了,我是你们的朋友全栈君。

这里是我的应用程序布局:

> 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()。

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144082.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号