Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call startActivityForResult from Fragment doesn't call onActivityResult

I have a DialogActivity which is called from a Fragment for show a custom Dialog with two image buttons.

In DialogActivity.onCreate

final Dialog dialog = new Dialog(this, R.style.DialogTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_pause); 
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();

In DialogActivity.onClick

        @Override
        public void onClick(View v) {
            Log.d(LOGTAG, "onClick CONTINUE");

            Intent resultData = new Intent();
            resultData.putExtra("TEST", "return data");
            setResult(666, resultData);
            dialog.cancel();
        }

In Fragment that calls startActivityForResult:

Intent dialogActivityIntent = new Intent(getActivity(), DialogActivity.class);
startActivityForResult(dialogActivityIntent, 999);

In Activity and Fragment that calls startActivityForResult:

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

    super.onActivityResult(requestCode, resultCode, data);
}

When I click the button I only get the dialog cancel and shows the background activity (fragment).

There isn't any call to onActivityResult, onResume, ... in the Fragment or the Activity contains the Fragment.

Things I tried:

To implement onActivityResult in both, Fragment and Activity that contains my Fragment.

Things to know:

I set the attribute noHistory=true in every Activity I have.

If I do finish() in onClick the Activity/Fragment that calls DialogActivity is closed too, and the application return to the before Activity.

This may be the problem, I DON'T call finish() ... but if I call finish(), it exits to another Activity, not the Activity that calls startActivityForResult.

Links I checked:

startActivityForResult() dont call to onActivityResult(int requestCode, int resultCode, Intent data)?

Cannot get to trigger onActivityResult() android?

startActivityForResult doesn't seem to call onActivityResult

onActivityResult never called

Android onActivityResult NEVER called

onActivityResult() not called when Activity started from Fragment


I hope everything is clearly explained ^^.

Thanks in advance.

like image 474
wendigo Avatar asked Dec 11 '12 17:12

wendigo


1 Answers

Activities that have the attribute noHistory=true will never have their onActivityResult() called when launching a new Activity via startActivityForResult(). As the documentation mentions, when the noHistory attribute is set to true, then finish() is called on the Activity when the user navigates away from the Activity.

So, when startActivityForResult() is called, the Activity is navigated away from, causing its finish() to be called and making it never receive a call to onActivityResult(). If you remove the noHistory=true attribute from the Activity that's calling startActivityForResult(), then call finish() in your DialogActivity's onClick(), then you should still see the Activity that launched it, as well as receive a call to onActivityResult().

like image 115
ashughes Avatar answered Oct 14 '22 00:10

ashughes