3

I have read many of the similar situations here on StackOverflow, as well as on Google, but none of them have helped me in my case. I have two activities, A and B. A calls B with an activity for result:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.mnuMainSettings:
            startActivity(new Intent(ActivityMain.this, ActivitySettings.class));
            break;
        case R.id.mnuMainHelp:
            startActivity(new Intent(ActivityMain.this, ActivityHelp.class));
            break;
        case R.id.mnuMainEULA:
            Intent intent = new Intent(ActivityMain.this, ActivityDisclaimer.class);
            startActivityForResult(intent, 1);
            break;
    }
    return true;
}

Activity B:

public class ActivityDisclaimer extends Activity
{

    private WebView webView;

    public static final int ACTIVITY_RESULT_DISCLAIMER = 1;
    public static final String ACTIVITY_RESULT = "result";
    public static final int ACTIVITY_RESULT_OK = 1;
    public static final int ACTIVITY_RESULT_CANCELED = 2;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disclaimer);

        webView = (WebView) findViewById(R.id.WebViewDisclaimer);
        webView.loadUrl("file:///android_asset/disclaimer.html");

    }

    public void onClickDisclaimerBtnAccept(View view) {
        Prefs prefs = new Prefs(this);
        prefs.setAcceptDisclaimer(true);
        prefs.commit();

        Intent returnIntent = new Intent();
        returnIntent.putExtra("result",true);
        setResult(Activity.RESULT_OK, returnIntent);        
        finish();
    }

    public void onClickDisclaimerBtnDecline(View view) {
        Prefs prefs = new Prefs(this);
        prefs.setAcceptDisclaimer(false);
        prefs.commit();

        Intent returnIntent = new Intent();
        returnIntent.putExtra("result",false);
        setResult(Activity.RESULT_OK, returnIntent);        
        finish();
    }

}

However, my onActivityResult method, the data returned with the intent is always NULL.

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // For response from disclaimer activity
        if (requestCode == 1) {

            if (resultCode == Activity.RESULT_OK){
                if (null != intent) {
                    Boolean accepted = intent.getBooleanExtra("result", false);
                    Log.d(TAG, "Boolean Data: " + accepted.toString());
                    if (!accepted) finish();
                } else { Log.d(TAG, "RESULT OK, DATA NULL"); }
            }
            if (resultCode == Activity.RESULT_CANCELED){
                if (null != intent) {
                    Boolean accepted = intent.getBooleanExtra("result", false);
                    Log.d(TAG, "Boolean Data: " + accepted.toString());
                    finish();
                } else { Log.d(TAG, "RESULT CANCELED, DATA NULL"); }
            }

        }
    }//onActivityResult

"RESULT CANCELED, DATA NULL" is all I ever get. Have I missed something here? Perhaps I have been staring at this code for too long!

Edit: To be more specific with my question, why am I getting a null return?

qkzoo1978
  • 315
  • 3
  • 11
  • In your IF loops, should you not check for `intent != null` and not `null != intent`? Also, in Activity B in the `setResult` - just put `RESULT_OK` without the Activity. – gilonm Mar 28 '14 at 02:17
  • Blocking out the finish does nothing. null != intent is a better choice, from what I've read. In any case, the null check is working fine, its why I'm getting the null result thats the issue. – qkzoo1978 Mar 28 '14 at 02:20
  • BTW, I see you have declared final vars above (result_OK, canceled...) but in the intents and your code you don't use them.. could it be related to that? (you declared `ACTIVITY_RESULT_OK` in a final variable, but in activity you used `Activity.RESULT_OK` – gilonm Mar 28 '14 at 02:24
  • gilonm No, had nothing to do with it. I had it with the finals originally, and then changed it to the way you see it now while troubleshooting. I've answered my own question just a minute ago. It all had to do with a line I had in my manifest file, launchMode="singleInstance", got rid of that and now everything works great. Not even sure why I put it in there to begin with. – qkzoo1978 Mar 28 '14 at 02:30
  • Thank you for your comments and suggestions! – qkzoo1978 Mar 28 '14 at 02:31

1 Answers1

6

Hot dog! To answer my own question, the above code is fine. I finally found the answer here: fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult

To summarize, I had android: launchMode="singleInstance" in my manifest.xml. I removed this line, and everything is working great now!

What a relief!

Community
  • 1
  • 1
qkzoo1978
  • 315
  • 3
  • 11