onResume() 是否在 onActivityResult() 之前调用?

发布于 2024-10-03 13:05:00 字数 2290 浏览 5 评论 0 原文

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

  1. onResume() 提示用户登录
  2. 如果用户登录,他可以继续使用该应用程序 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( ) 首先被调用。

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

Here is how my app is laid out:

  1. onResume() user is prompted to login
  2. If user logs in, he can continue using the app
    3. If the user logs out at any time, I want to prompt login again

How can I achieve this?

Here is my MainActivity:

@Override
    protected void onResume(){
        super.onResume();

        isLoggedIn = prefs.getBoolean("isLoggedIn", false);

        if(!isLoggedIn){
            showLoginActivity();
        }
    }

Here is my 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);
            }
        }
    }

After the user has successfully logged in:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(getApplicationContext(), "BOOM SHAKA LAKA!",Toast.LENGTH_SHORT).show();
        }
    }

The problem is, onResume() is called before onActivityResult() so when the user has successfully logged in, my main activity does not get notified because onResume() gets called first.

Where is the best place to prompt for login?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£冰雨忧蓝° 2024-10-10 13:05:00

实际上,对 onActivityResult 的调用发生在 onResume 之前(请参阅 文档)。您确定确实使用 startActivityForResult 启动了所需的 Activity,并且在向 Activity 返回值之前将调用的 Activity 的结果设置为 RESULT_OK 吗?尝试在 onActivityResult 中添加一个 Log 语句来记录该值并确保它被命中。另外,您在哪里设置 isLoggedIn 首选项的值?看起来您应该在登录活动返回之前将其设置为 true ,但这显然没有发生。

编辑

文档说:

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

The call to onActivityResult happens before onResume, actually (see the docs). Are you sure you're actually starting the activity you wanted with startActivityForResult and that you're setting the result of the invoked activity to RESULT_OK before returning a value to your activity? Try just putting a Log statement in your onActivityResult to log that value and make sure that gets hit. Also, where are you setting the value of the isLoggedIn preference? It seems like you should be setting that to true in your login activity before it returns anyways, but that's clearly not happening.

Edit

The docs say:

You will receive this call immediately before onResume() when your activity is re-starting.

养猫人 2024-10-10 13:05:00

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

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

With fragments it isn't even as simple as onActivityResult() being called before the call to onResume(). If the activity that you are returning to was disposed of in the interim, you will find that calls to (for example) getActivity() from onActivityResult() will return null. However, if the activity has not been disposed of, a call to getActivity() will return the containing activity.

This inconsistency can be a source of hard-to-diagnose defects but you can check the behaviour of your application by enabling the developer option "Don't keep activities". I tend to keep this turned on - I'd rather see a NullPointerException in development than in production.

讽刺将军 2024-10-10 13:05:00

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

You may want to consider abstracting away the login state from the activity. For example if a user can post comments, let the onPost action ping for login state and go from there, instead of from the activity state.

蓝颜夕 2024-10-10 13:05:00

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

Callback methods like onResume are not suitable places to achieve the requested functionality.
I would suggest making a class & adding the sign-in/sign-out functionality there.
when a signout callback is received, then call the sign-in functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文