Call startActivityForResult from Fragment doesn't call onActivityResult

25,577

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

Share:
25,577
wendigo
Author by

wendigo

Updated on August 20, 2022

Comments

  • wendigo
    wendigo over 1 year

    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.

  • wendigo
    wendigo almost 11 years
    Sorry for the late comment... I solved this problem some days later doing what you say. Thanks!
  • dcool
    dcool over 10 years
    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.
  • Admin
    Admin over 2 years
    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.
  • ericmas001
    ericmas001 over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review