这里是我的应用程序奠定了出来:

  1. onResume()提示用户登录
  2. 如果用户登录,他可以继续使用的应用程序 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,实际上(见 文档).你确定你真正开始的活动有你想要的 startActivityForResult 和你设定的结果调活动 RESULT_OK 在返回前一个值您的活动?尝试只是把一个 Log 在你的声明 onActivityResult 登录的价值并确保获取的打击。还有,你在哪里设置的价值 isLoggedIn 偏好吗?好像你应该被设置到 true 在你登录活动在返回之前不管怎么说,但是这显然没有发生。

编辑

文档 说:

你会接受这一呼吁立即前onResume()当您的活动是重新开始。

其他提示

与碎片它甚至不是一样简单 onActivityResult() 被称为之前的呼叫 onResume().如果该活动是回到被安置在临时,你会发现,呼吁以(例如) getActivity()onActivityResult() 将返回null。然而,如果活动没有设置的,一个呼叫 getActivity() 将返回的含的活动。

这种不一致可能是一个来源很难诊断的缺陷但是你可以检查行为的应用程序使开发商选择"不要让活动"。我倾向于保持这个打开-我宁愿看到一个 NullPointerException 在发展中比在生产。

你可能想要考虑抽象离开登录的国家的活动。例如,如果一个用户可以发表意见,让onPost平行动用于登录的国家,并从那里去,而不是从活动的状态。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top