Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: onActivityResult data always null

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?

like image 576
qkzoo1978 Avatar asked Dec 29 '25 19:12

qkzoo1978


1 Answers

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!

like image 174
qkzoo1978 Avatar answered Dec 31 '25 07:12

qkzoo1978