onActivityResult前onResume()被称为()? [英] Is onResume() called before onActivityResult()?

查看:338
本文介绍了onActivityResult前onResume()被称为()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是如何我的应用程序的布局:

Here is how my app is laid out:

  1. 在onResume()会提示用户登录
  2. 如果用户登录时,他可以继续使用的应用程序 3。如果用户注销在任何时候,我想再次提示登录
  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?

下面是我的MainActivity:

Here is my MainActivity:

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

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

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

下面是我的LoginActivity:

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);
            }
        }
    }

在用户成功登录:

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()被调用第一。

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?

推荐答案

onResume之前onActivityResult的通话情况,其实(见的的文档)。你确定你真正开始你想与 startActivityForResult 的活动,你设置调用活动的结果为 RESULT_OK 的值返回到你的活动之前?尽量只把一个登录语句在 onActivityResult 登录该值,并确保被击中。此外,你在哪里设置的 isLoggedIn preference的价值?这似乎是你应该设置,为在你登录活动之前返回反正,但是这显然不会发生。

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.

这篇关于onActivityResult前onResume()被称为()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆