onActivityResult() not called after startActivityF

2019-07-21 08:44发布

问题:

I got my main Activity which holds different Fragment's, one fragment gives the user a possibility to open a DialogFragment. That dialog opens a list of sound files and the dialog also contains a 'Add' button from which the user should be able to add her own soundfile. To do this I thought to use the standard Android file picking functionality and make use of the Intent.ACTION_GET_CONTENT. So I fire away this intent when the user presses the 'Add' button and the Android file picking functionality comes up as it should, but when I choose a file, nothing happens what so ever. I would expect the onActivityResult()of my main activity to be triggered but it doesn't, and I can't get it to work and I don't understand why this doesn't work.

public class MyDialog extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Rest omitted...
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(context)
            .setIcon(android.R.drawable.ic_dialog_info) 
            .setTitle(R.string.TONE_PROMPT_TITLE)       

            .setNeutralButton(R.string.ADD, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.setType("audio/mpeg");
                    getActivity().startActivityForResult(intent, ADD_SOUND_REQUEST_CODE);
                }
            })

            .create();
}

}

I have also tried to instantiate the intent the following way: Intent intent = new Intent(getActivity(), MyActivity.class); but this doesn't work either.

Any ideas would be really helpful, thanks!

回答1:

Managed to figure out what the problem was... In my manifest I have declared my Activity as android:noHistory="false" and this was the problem. After setting it to android:noHistory="true" for my activity and firing my Intentthe following way it worked like a charm.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");           
getActivity().startActivityForResult(intent, ADD_SOUND_REQUEST_CODE);

And just like that, the onActivityResult() in my activity was triggered just as it should and after that I followed @Rohit5k2's advice on how to get my Fragment's OnActivityResult() triggered and all has worked really great!

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    MyFragment fragmentObject = getSupportFragmentManager().findFragmentById(R.id.container);
    fragmentObject.onActivityResult(requestCode, resultCode, data);
}

Thanks guys for your help!



回答2:

Do this in your activity which holds the fragments

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    MyFragment fragmentObject = getSupportFragmentManager().findFragmentById(R.id.container);
    fragmentObject.onActivityResult(requestCode, resultCode, data);
}

then onActivityResult() in your fragment will be called.



回答3:

In fragments you'd generally use this.startActivityForResult(...) and override the fragment's method onActivityResult(...). Since your case is a DialogFragment this would be useless as the dialog would be destroyed after pressing one of its buttons.

That means you correctly called getActivity().startActivityForResult(...) but since the caller is the activity, the activity is the one that has to implement the onActivityResult(...) method. No fragments receive the callback when taking this approach.