简体   繁体   English

resultCode始终为0

[英]resultCode is always 0

I'm trying to get the resultCode to be OK inside my onActivityResult function. 我正在尝试在onActivityResult函数中使resultCode正常。 However, it keeps coming back as 0. I have spent several days on this, and can't figure out why it doesn't work. 但是,它一直返回为0。我花了几天时间,无法弄清楚为什么它不起作用。 Here's my code. 这是我的代码。 If anybody can help me, I'll be very grateful, Thanks. 如果有人可以帮助我,我将非常感谢,谢谢。

My Activity1 class: 我的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
                }
        }

My Activity 2 class: 我的活动2课:

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

My manifest: 我的清单:

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

is the problem. 是问题。 According to docs: 根据文档:

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

finish() is actually not one of the callbacks you get as part of the activity lifecycle. finish()实际上不是您在活动生命周期中获得的回调之一。 Can you confirm that you're calling finish() yourself within MyActivity2. 您可以确认自己在MyActivity2中正在调用finish()吗?

If you're not calling finish() by yourself, you should call setResult(), within the the onDestroy() of MyActivity2. 如果您不是自己调用finish(),则应在MyActivity2的onDestroy()中调用setResult()。

Hope this helps! 希望这可以帮助!

If you call finish() for your Activity it will be always 0 as result ( http://developer.android.com/reference/android/app/Activity.html#finish() ) 如果您为Activity调用finish(),则结果始终为0( http://developer.android.com/reference/android/app/Activity.html#finish()

There is another method exists: finishActivity(int requestCode). 存在另一种方法:finishActivity(int requestCode)。

But if you want put Extras in Result we need other way. 但是,如果您想将Extras放入Result中,我们需要其他方法。 So why do you override finish() method? 那么,为什么要覆盖finish()方法呢? Where from do you call this method? 您从哪里调用此方法?

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

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