简体   繁体   English

在`finish()`之后没有调用onActivityResult - 调用被调用的活动

[英]onActivityResult not being called after `finish()`-ing the called activity

I know, I know... This problem is all over SO already.我知道,我知道......这个问题已经解决了。 None of those solutions worked for me / applied to me.这些解决方案都不适合我/适用于我。

Here is how I try to do it.这是我尝试做的方法。

From LoginActivity (Calling Activity ):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);
        }
    }

Then in PinCodeActivity (Called Activity )然后在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();
    }

Then LoginActivity.onActivityResult() EDIT: idk if important but it doesn't matter in my case whether super.onActivityResult() is called first or last.然后LoginActivity.onActivityResult()编辑:idk 如果重要,但在我的情况下,首先还是最后调用super.onActivityResult()并不重要。 They both result in the same.它们都导致相同的结果。

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

This is how LoginActivity is defined这就是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...
}

And finally how PinCodeActivity is defined最后是如何定义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...
}

Here is the 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)

Note the line with all the === which is printed right before finish() and right after setResult() this means it was able to set the result.请注意所有===的行,它在finish()之前和setResult()之后打印,这意味着它能够设置结果。 Also I don't see signs of errors/crashes looking at those logs.此外,我看不到这些日志的错误/崩溃迹象。

My problem is as the title says, onActivityResult() is not called from activity that I started with startActivityForResult()我的问题是正如标题所说,onActivityResult() 不是从我以 startActivityForResult() 开始的活动中调用的

Maybe it's just the way you phrased that, but your code doesn't clarify, so let's be clear.也许这只是你表达的方式,但你的代码没有澄清,所以让我们说清楚。 onActivityResult() should NOT be called "from activity that [you] started". onActivityResult()不应被称为“来自 [您] 开始的活动”。 onActivityResult is called in the activity that you originally called startActivityForResult from . onActivityResult在您最初调用startActivityForResult的活动中被调用

So your onActivityResult should be declared in LogInActivity NOT in PinCodeActivity .所以你的onActivityResult应该在LogInActivity不是PinCodeActivity中声明。

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM