14

I would like to add this to another list of questions about resultCode == 0 and requestCode == 0.

Here is the synopsis: NoteActivity calls NoteSettingsActivity using startActivityForResult().

I have searched the web and when I pressed back button when super.onBackPressed(), the resultCode == 0. Well after further researching, it seems it returns this code whenever the back button is pressed, but after botching that super.onBackPressed() call and just simply finish() the application the onActivityResult()'s resultCode is still equals to 0. It goes the same with requestCode.

Also, I tried manipulating the manifest file, I have done so many changes just to get this work but nothings works for me.

Here is the snippet. Note that I have reverted back to my previous commit so I have lost my recent modifications, but please take a look on the code I have wrote before I notice that the resultCode is always equals to 0 (ACTIVITY_CANCELED)

@Override
public void onNoteSettingsActivityCalled(Note note)
{
    Intent intent = new Intent(this, NoteSettingsActivity.class);
    intent.putExtra(NoteExtrasKey.EXTRA_NOTE_ID, note.getNoteID());

    startActivityForResult(intent, NoteRequest.REQUEST_UPDATE_SETTINGS);
}

Here is when the activity detected back press:

@Override
public void onBackPressed()
{   
    Log.i(NoteApplication.TAG, "NoteSettingsActivity.onBackPressed() has been called.");

    Intent intent = new Intent();
    intent.putExtra(NoteExtrasKey.EXTRA_NOTE_REMINDENABLED , mRemindEnabled);
    intent.putExtra(NoteExtrasKey.EXTRA_NOTE_REMINDEVERY   , mDaysSelected);
    intent.putExtra(NoteExtrasKey.EXTRA_NOTE_REMINDON      , String.valueOf(mRemindDateTime));
    intent.putExtra(NoteExtrasKey.EXTRA_NOTE_ID            , mTargetNoteID);

    if(getParent() != null)
        getParent().setResult(Activity.RESULT_OK, intent);
    else
        setResult(Activity.RESULT_OK, intent);

    super.onBackPressed();
}

Here's how NoteActivity received the resulting call.

    @Override
public void onActivityResult(int result, int request, Intent intent)
{
    super.onActivityResult(result, request, intent);

    Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() has been called.");
    Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() result = " + result + " request = " + request);

    if(result == Activity.RESULT_CANCELED)
        return;

    switch(request)
    {
        case NoteRequest.REQUEST_UPDATE_SETTINGS:

            if(intent == null) return;

            int noteID = intent.getIntExtra(NoteExtrasKey.EXTRA_NOTE_ID, -1);
            String remindOnString = intent.getStringExtra(NoteExtrasKey.EXTRA_NOTE_REMINDON);

            if(remindOnString != null && !remindOnString.equals(""))
                mRemindDateTime = Timestamp.valueOf(remindOnString);

            mHasSettingsEnabled = true;
            mRemindEnabled = intent.getBooleanExtra(NoteExtrasKey.EXTRA_NOTE_REMINDENABLED, false);
            mSelectedDays = intent.getIntegerArrayListExtra(NoteExtrasKey.EXTRA_NOTE_REMINDEVERY);

            if(noteID < 0)
            {
                Note note = mNoteDatabaseHelper.getNote(noteID);
                note.setRemindEnabled(mRemindEnabled);
                note.remindEvery(mSelectedDays);
                note.remindOn(mRemindDateTime);

                onNoteItemUpdated(note); 
            }

            Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() NoteRequest.REQUEST_UPDATE_SETTINGS called.");

            break;

        default:
            Log.i(NoteApplication.TAG, "NoteActivity.onActivityResult() : unknown request code = " + request);
            break;
    }
}

resultCode equals 0 and requestCode requals -1 when I ran this. I have checked the intent passed on this and it is not null.

Here are the questions very related to this question. None of them worked:

I am losing a lot of important hours working on my project just figuring out what makes the value for resultCode and requestCode lose the value I sent along the way.

Any help and guidance will be appreciated. Thank you very much!

Community
  • 1
  • 1
Neon Warge
  • 1,817
  • 6
  • 29
  • 53

6 Answers6

29

In my case, I got the resultCode == 0 error because of two reasons.

Firstly, I copied the method from a fragment instead of from another activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

A good way to prevent this from happening is to @Override your method so that you know you are overriding the default activity onActivityResult method signature.

Use the correct signature:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

Secondly, I was doing this:

    super.onBackPressed();
    Intent intent = new Intent();
    intent.putExtra(getString(R.string.like_count), people.size());
    Log.e(TAG, people.size() +" people ");
    setResult(RESULT_OK, intent);
    finish();

The super.onBackPressed() set the resultCode before it was actually set hence it resulted in the 0.

A simple fix cause it to work properly:

    Intent intent = new Intent();
    intent.putExtra(getString(R.string.like_count), people.size());
    Log.e(TAG, people.size() +" people ");
    setResult(RESULT_OK, intent);
    super.onBackPressed();
Simon
  • 19,658
  • 27
  • 149
  • 217
  • It saves my so much time and tention..:) use `super.onBackPressed();` – AndyBoy Nov 09 '16 at 06:38
  • I am not sure if IIRC, there was once a time I trusted the intellisense in previous version in Android Studio and by god I swear it highlighted the first param to be filled in as 'resultCode' and then followed by 'requestCode' so maybe that is why. – Neon Warge Apr 04 '18 at 00:15
  • nice and clean answer +1 – Erfan Jan 05 '21 at 08:16
4

The call to super.onBackPressed() is the one that will in the end set the result code and send it back to the calling activity. Replace that call with a call to finish() and you should get the result code that you are looking for.

Hope this helps!

k3v1n4ud3
  • 2,904
  • 1
  • 15
  • 19
  • On the link I have posted, I did similar thing. That doesn't work. – Neon Warge Apr 30 '15 at 15:50
  • Yep, confirmed, I have tried as suggested. It did not work as well. I will try to see if this is reproducible when I create a button and setResult from there. – Neon Warge Apr 30 '15 at 15:53
  • Is the getParent() call really necessary here? If NoteActivity is the calling activity and NoteSettingsActivity is the called one, you can just call setResult() and not getParent().setResult() and then call finish() to terminate the NoteSettingsActivity. Have you tried that? – k3v1n4ud3 Apr 30 '15 at 15:58
  • 1
    I personally always used `setResult()` and then `finish()` and it worked. Do not use `super.onBackPressed()`. – shkschneider Apr 30 '15 at 16:04
  • I have done it as well. I mentioned that was on previous commit. But that doesn't work as well. – Neon Warge Apr 30 '15 at 16:07
4

The correct signature is

public void onActivityResult(int requestCode, int resultCode, Intent data) {

You have request and result mixed up.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • I was just a bit tired. I could have noticed! Thank you very much! You saved my life. – Neon Warge Apr 30 '15 at 16:36
  • I did not mix up the signature, but I kept checking resultCode and thought I was looking requestCode, hope it would help someone who comes to look for the answer later. – Dino Tw Dec 08 '17 at 23:23
1

Was facing the same issue while doing uninstalling an application from the device. It got resolved by putting: intentObject.putExtra(Intent.EXTRA_RETURN_RESULT, true); before you do startActivityForResult.

AVINASH SHRIMALI
  • 580
  • 6
  • 11
0

For the comers from The Android Big Nerd Ranch Book, it's probably a typo in your code, make sure you're comparing correctly:

if (requestCode == REQUEST_CODE_CHEAT) {
if (RESULT_OK == resultCode) {...} 
...
}

Reminder: RESULT_OK is always -1 and it's existence means setResult() was called from the child Activity.

Yidir
  • 583
  • 7
  • 12
0

I know I am late to the party, but: I had the sam problem, for me it did not work because I was calling onBackPressedDispatcher.onBackPressed() before setting the result.

Like this:

override fun onBackPressed() {
    onBackPressedDispatcher.onBackPressed() // <---- PROBLEM
    val intent = Intent()
    intent.putExtra("KEY", "VALUE"))
    setResult(RESULT_OK, intent);
    // AND here I tried with this calls without luck. -> requireActivity().finish() or super.onBackPressed()
}
I.Step
  • 613
  • 6
  • 22