代码之家  ›  专栏  ›  技术社区  ›  Sheehan Alam

OnResume()是否在OnActivityResult()之前调用?

  •  74
  • Sheehan Alam  · 技术社区  · 15 年前

    以下是我的应用程序的布局:

    1. 提示onresume()用户登录
    2. 如果用户登录,他可以继续使用该应用程序 三。如果用户在任何时候注销,我想再次提示登录

    我怎样才能做到这一点?

    以下是我的主要活动:

    @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()首先被调用。

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

    3 回复  |  直到 11 年前
        1
  •  88
  •   Yoni Samlan    15 年前

    实际上,对OnActivityResult的调用发生在OnResume之前(请参见 the docs )你确定你正在开始你想要的活动吗? startActivityForResult 并且您将被调用活动的结果设置为 RESULT_OK 在为您的活动返回值之前?试着把 Log 你的陈述 onActivityResult 记录这个值并确保它被击中。另外,您在哪里设置 isLoggedIn 偏爱?看起来你应该把它 true 在您的登录活动之前,它会以任何方式返回,但这显然不会发生。

        2
  •  28
  •   Phil Haigh    13 年前

    对于碎片来说,它甚至不像 onActivityResult() 在呼叫前被呼叫到 onResume() . 如果您要返回的活动在过渡期间被处理,您将发现该调用(例如) getActivity() OnActivity结果() 将返回空值。但是,如果活动未被释放,则调用 获取活动() 将返回包含的活动。

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

        3
  •  2
  •   sgarman    15 年前

    您可能需要考虑从活动中抽象出登录状态。例如,如果用户可以发布注释,那么让onpost操作ping进入登录状态,然后从登录状态开始,而不是从活动状态开始。