【问题标题】:Is onResume() called before onActivityResult()?是否在 onActivityResult() 之前调用了 onResume()?
【发布时间】:2011-05-14 06:41:50
【问题描述】:

这是我的应用的布局方式:

  1. onResume() 用户被提示登录
  2. 如果用户登录,他可以继续使用该应用程序 3。如果用户随时退出,我想再次提示登录

我怎样才能做到这一点?

这是我的 MainActivity:

@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() 是先调用的。

提示登录的最佳位置在哪里?

【问题讨论】:

    标签: android android-activity android-lifecycle activity-lifecycle


    【解决方案1】:

    实际上,对 onActivityResult 的调用发生在 onResume 之前(请参阅the docs)。您确定您实际上是在使用startActivityForResult 开始您想要的活动,并且您在将值返回给您的活动之前将调用活动的结果设置为RESULT_OK?试着在你的onActivityResult 中加入一个Log 语句来记录该值并确保它被命中。另外,您在哪里设置 isLoggedIn 首选项的值?看来您应该在登录活动中将其设置为true,然后再返回,但这显然没有发生。

    编辑

    The docs说:

    当您的活动重新开始时,您将在 onResume() 之前立即收到此调用。

    【讨论】:

    • 我在用户登录后设置 isLoggedIn。查看我更新的代码。不知道怎么了?
    • 你是对的,onActivityResult() 在 onResume() 之前被调用。不知道为什么我的偏好被读错了?
    • 注意onActivityResult也是在onStart之前调用的,这被很多人认为是bug:code.google.com/p/android/issues/detail?id=17787
    【解决方案2】:

    对于片段,它甚至不像在调用onResume() 之前调用onActivityResult() 那样简单。如果您要返回的活动在此期间被处理掉,您会发现从 onActivityResult() 调用(例如)getActivity() 将返回 null。但是,如果该活动尚未被释放,则对getActivity() 的调用将返回包含活动。

    这种不一致可能会导致难以诊断的缺陷,但您可以通过启用开发人员选项“不保留活动”来检查应用程序的行为。我倾向于保持打开状态 - 我宁愿看到 NullPointerException 在开发中而不是在生产中。

    【讨论】:

      【解决方案3】:

      您可能需要考虑从活动中抽象出登录状态。例如,如果用户可以发布 cmets,则让 onPost 操作 ping 登录状态并从那里开始,而不是从活动状态开始。

      【讨论】:

        【解决方案4】:

        onResume 这样的回调方法不适合实现所请求的功能。 我建议开设一个课程并在那里添加登录/注销功能。 当收到注销回调时,调用登录功能。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-15
          • 1970-01-01
          • 2018-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多