javaandroidexplicit-intent

Activity intent child doesn't return data to parent, toast is not shown


Greetings fellow programmers,

I have been having some issues with understanding why my child's intent doesn't return the proper value to my parent's intent properly.

private static final int REQUEST_CODE_CHEAT = 0;

Now here is how I call my explicit intent

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        boolean isTrue = mStatements[mStatementIndex].isAnswerTrue();
        Intent intent = SubActivity.newIntent(MainActivity.this, isTrue);
        startActivityForResult(intent, REQUEST_CODE_CHEAT);
    }
});

This is the code for the SubActivity that creates the interfacing Intent

public static Intent newIntent(Context packageContext, boolean answerIsTrue){
    Intent intent = new Intent(packageContext, SubActivity.class);
    intent.putExtra(EXTRA_ANSWER_SHOWN, answerIsTrue);
    return intent;
}

Also, have the following keys to keep track of what goes where

private static final String EXTRA_ANSWER_IS_TRUE = "com.tsourtzis.android.test.answer_is_true";
private static final String EXTRA_ANSWER_SHOWN = "com.tsourtzis.android.test.answer_shown";

Finally some methods to be certain what values are passed

private void setAnswerShownResult(boolean isAnswerShown){
    Intent data = new Intent();
    data.putExtra(EXTRA_ANSWER_IS_TRUE, isAnswerShown);
    setResult(RESULT_OK, data);
}

Here I'm sending back the intent to the MainActivity with setResult();

public static boolean wasAnswerShown(Intent result){
    return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
}

Moreover, I have this method here to evaluate the result, but checking first if the user has created it.

public void evaluateStatement(boolean userChoice){
    boolean statementValue = mStatements[mStatementIndex].isAnswerTrue();

    int textResId = 0;

    if(mBoolResult){
        textResId = R.string.judgment_toast;
    }else{
        if(userChoice == statementValue){
            textResId = R.string.correct_toast;
        }else{
            textResId = R.string.incorrect_toast;
        }
    }

    Toast.makeText(this, textResId, Toast.LENGTH_SHORT).show();
}

Notice, I have stated the following my MainActivity after using setResult(...) and a field value for keeping the boolean result from the Sub activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode != Activity.RESULT_OK){
        return;
    }

    if(requestCode == REQUEST_CODE_CHEAT){
        if(data == null){
            return;
        }

        mBoolResult = SubActivity.wasAnswerShown(data);
    }
}

Finally just to be clear, here is the field that I have in MainActivity.

private boolean mBoolResult;

I'm not looking for any code answer, I just want to understand why it doesn't work. Thank you!


Solution

  • Do you finish() your SubActivity ?

    Seeing that the Toast should be shown any time the method evaluateStatement is called, I guess onActivityResult doesn't dispatch the call to it.

    Can you explicit where is the onActivityResult you show last? It's unclear since it's calling CheatActivity#wasAnswerShown() as if it is not in CheatActivity (MainActivity?)