繁体   English   中英

resultCode始终为0

[英]resultCode is always 0

我正在尝试在onActivityResult函数中使resultCode正常。 但是,它一直返回为0。我花了几天时间,无法弄清楚为什么它不起作用。 这是我的代码。 如果有人可以帮助我,我将非常感谢,谢谢。

我的Activity1课:

    private class MyTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
                // process
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            Intent i = new Intent(Activity1.this, Activity2.class);
            i.putExtra("Value1", "This value one for ActivityOne ");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityForResult(i, REQUEST_CODE);
            textView.setText(result);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
                       // do something
                }
        }

我的活动2课:

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra("returnKey1", "return 1");
        setResult(RESULT_OK, data);
        super.finish();
    }

我的清单:

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity2"
                  android:label="@string/app_dialog_name" 
                  android:launchMode="singleTop" 
                  android:excludeFromRecents="true"
              android:taskAffinity="" 
          android:theme="@android:style/Theme.Dialog">
        </activity>
    </application>
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

是问题。 根据文档:

This flag can not be used when the caller is requesting a result from the activity being launched.

finish()实际上不是您在活动生命周期中获得的回调之一。 您可以确认自己在MyActivity2中正在调用finish()吗?

如果您不是自己调用finish(),则应在MyActivity2的onDestroy()中调用setResult()。

希望这可以帮助!

如果您为Activity调用finish(),则结果始终为0( http://developer.android.com/reference/android/app/Activity.html#finish()

存在另一种方法:finishActivity(int requestCode)。

但是,如果您想将Extras放入Result中,我们需要其他方法。 那么,为什么要覆盖finish()方法呢? 您从哪里调用此方法?

暂无
暂无

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

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