简体   繁体   中英

resultCode is always 0

I'm trying to get the resultCode to be OK inside my onActivityResult function. However, it keeps coming back as 0. I have spent several days on this, and can't figure out why it doesn't work. Here's my code. If anybody can help me, I'll be very grateful, Thanks.

My Activity1 class:

    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:

    @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. Can you confirm that you're calling finish() yourself within MyActivity2.

If you're not calling finish() by yourself, you should call setResult(), within the the onDestroy() of MyActivity2.

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

There is another method exists: finishActivity(int requestCode).

But if you want put Extras in Result we need other way. So why do you override finish() method? Where from do you call this method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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