15

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.

Community
  • 1
  • 1
wendigo
  • 1,973
  • 2
  • 17
  • 21
  • 2
    Try `getActivity().startActivityForResult(dialogActivityIntent, 999);` and catch the result in the `Activity` which hosts the `Fragment` – Ole Dec 11 '12 at 17:49
  • I have tried that, the code was that just a minute before post my ask. Thanks :( – wendigo Dec 11 '12 at 18:03
  • 1
    Does the `Activity` you want the result from does have `android:launchMode` specified to `singleInstance` or `singleTask` in the manifest file? – Ole Dec 11 '12 at 18:18
  • 1
    See this link may help you:https://www.androidtutorialonline.com/onactivityresult-in-fragment/ – Android Tutorial Sep 17 '18 at 07:48

2 Answers2

17

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().

ashughes
  • 7,155
  • 9
  • 48
  • 54
  • Sorry for the late comment... I solved this problem some days later doing what you say. Thanks! – wendigo Jun 07 '13 at 10:40
  • great @ashu.. it solved my two problems first my activity was getting closed as soon as display turned off secondly the same problem mentioned in the question..thanks for the help. – dcool Jan 01 '14 at 17:39
0

Close all the activities after you are done with the result. For example, in your overridden onActivityResult you can add finishActivity (activity 1..N). Don't forget to place each under try catch.

yandiz
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '21 at 09:14
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30669944) – ericmas001 Dec 27 '21 at 11:52