在完成(finish())之后调用onActivityResult-调用的活动

时间:2019-11-09 17:44:27

标签: java android androidx

我知道,我知道...这个问题已经解决了。这些解决方案都不对我有用。

这是我尝试做到的方式。

来自LoginActivity(呼叫Activity):

    private void loginSuccessful(LoggedInUserView model) {
        String welcome = String.format(getString(R.string.welcome), model.getDisplayName());
        Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this, PinCodeActivity.class);
        if (!model.isNewUser()) {
            intent.putExtra("prompt", "Enter your pin");
            startActivityForResult(intent, 102);
        } else {
            intent.putExtra("prompt", "Enter a pin");
            startActivityForResult(intent, 103);
        }
    }

然后在PinCodeActivity中(称为Activity

    private void handlePinEntered() {
        Intent intent = new Intent();
        intent.putExtra("hash", pin.getValue());
        setResult(Activity.RESULT_OK, intent);
        System.out.println("==================================="); // Is printed
        finish();
    }

然后LoginActivity.onActivityResult()编辑:idk如果重要,但是对于我来说super.onActivityResult()是第一个还是最后一个都无关紧要。它们都导致相同的结果。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println(requestCode); // Is NOT printed
        if (resultCode == Activity.RESULT_OK)
            switch (requestCode) {
                case 101:
                    try {
                        // The Task returned from this call is always completed, no need to attach
                        // a listener.
                        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
                        GoogleSignInAccount account = task.getResult(ApiException.class);
                        loginViewModel.login(account);
                    } catch (ApiException e) {
                        // The ApiException status code indicates the detailed failure reason.
                        Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
                    }
                    break;
                case 102:
                    System.out.println(data.getStringExtra("hash"));
                    break;
                case 103:
                    System.out.println(data.getStringExtra("hash"));
                    break;
            }
        super.onActivityResult(requestCode, resultCode, data);
    }

这是LoginActivity的定义方式

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LOGIN";

    @Inject
    DaggerViewModelFactory viewModelFactory;
    @Inject
    AppProperties appProperties;
    private LoginViewModel loginViewModel;

    private ProgressBar loadingProgressBar;

    private EditText usernameEditText;
    private EditText passwordEditText;

    private Button loginButton;
    private SignInButton googleLoginButton;
    private GoogleSignInClient googleSignInClient;
// more code...
}

最后是PinCodeActivity的定义方式

public class PinCodeActivity extends AppCompatActivity implements View.OnClickListener {
    private List<RadioButton> unchecked;
    private List<RadioButton> checked = new LinkedList<>();
    private PinCode pin = new PinCode();
// more code...
}

这是logcat

2019-11-09 19:26:08.000 2462-2535/com.example.finance D/EGL_emulation: eglMakeCurrent: 0x7fb761429ae0: ver 3 0 (tinfo 0x7fb761418f40)
2019-11-09 19:26:08.009 2462-2535/com.example.finance D/EGL_emulation: eglMakeCurrent: 0x7fb761429ae0: ver 3 0 (tinfo 0x7fb761418f40)
2019-11-09 19:26:10.506 2462-2462/com.example.finance I/System.out: ===================================
2019-11-09 19:26:11.069 2462-2535/com.example.finance D/EGL_emulation: eglMakeCurrent: 0x7fb761429ae0: ver 3 0 (tinfo 0x7fb761418f40)

请注意,所有===都在finish()之前和setResult()之后打印,这意味着它可以设置结果。 另外,查看这些日志也看不到错误/崩溃的迹象。

1 个答案:

答案 0 :(得分:1)

  

我的问题就像标题所说的那样,我从以startActivityForResult()开始的活动中未调用onActivityResult()

也许这只是您所说的方式,但是您的代码不清楚,所以让我们清楚一点。 onActivityResult()不应被称为“从[您]开始的活动中”。在您最初从{em>中调用onActivityResult的活动中,startActivityForResult被称为

因此,您的onActivityResult应该在LogInActivity中声明,而不要在PinCodeActivity中声明。

希望有帮助!