简体   繁体   English

startActivityForResult() 返回 RESULT_CANCELED

[英]startActivityForResult() returns RESULT_CANCELED

I have 2 Activity classes and 1 Non-Activity class which calls startActivityForResult() from Context passed in constructor.我有 2 个 Activity 类和 1 个非 Activity class,它从构造函数中传递的 Context 调用 startActivityForResult()。 This is how it looks: FirstActivity -> NonActivity -> SecondActivity -> FirstActivity.它看起来是这样的:FirstActivity -> NonActivity -> SecondActivity -> FirstActivity。 In SecondActivity there is ArrayList of custom objects that needs to be passed to FirstActivity as a result.结果,在 SecondActivity 中有 ArrayList 个自定义对象需要传递给 FirstActivity。 There is a problem.这儿存在一个问题。 When onActivityResult() is called resultCode is RESULT_CANCELED, but not RESULT_OK even if setResult(RESULT_OK, intent) is called.当调用 onActivityResult() 时,resultCode 是 RESULT_CANCELED,但不是 RESULT_OK,即使调用了 setResult(RESULT_OK, intent)。 Here is my code:这是我的代码:

NonActivity非活动

public void showActivity() {
    Intent intent = new Intent(request, ActivityKorak.class);
    intent.putExtra("data", fields);

    request.startActivityForResult(intent, 1);
}

SecondActivity第二活动

@Override
public void onBackPressed() {
    super.onBackPressed();

    Intent intent = new Intent();
    intent.putExtra("data", fields);
    setResult(Activity.RESULT_OK, intent);

    finish();
}

FirstActivity第一个活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(resultCode != Activity.RESULT_CANCELED){
        if(requestCode == 1) {
            Bundle extras = intent.getExtras();
            ArrayList<CustomInput> fields = (ArrayList<CustomInput>) extras.getSerializable("data");
        }
    }
}

You must simply remove你必须简单地删除

super.onBackPressed();

in the onBackPressed Method在 onBackPressed 方法中

What is happening is that "super.onBackPressed()" is setting the result code to "RESULT_CANCELED" and finishing your activity.发生的事情是“super.onBackPressed()”正在将结果代码设置为“RESULT_CANCELED”并完成您的活动。

In my case, the problem was fixed by adding the SHA-1 and SHA-256 certificate fingerprints given in Android Studio (click on Gradle on the right side of the AS window, then run configurations and signingReport) to your Firebase project settings->General->SDK setup and configuration.就我而言,通过将 Android Studio 中给出的 SHA-1 和 SHA-256 证书指纹(单击 AS window 右侧的 Gradle,然后运行配置和签名报告)添加到您的 Firebase 项目设置->常规 -> SDK 设置和配置。

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

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