2

Hy I am developing an Android Application that contains one host activity with multiple fragments. I was using onActivityResult() method in my host Activity as well as in my fragments.

In host Activity

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

In fragment

public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);        
     switch (requestCode)
    {
    }
}

When I started activityForResult from fragment then fragment's onActivityResult was not getting called. I was doing it like this

startActivityForResult(Intent.createChooser(intent, "Select File"),REQUEST_CODE);

I solved my problem when I added this line to my host activity's result method super.onActivityResult(requestCode, resultCode, data);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
    }
}

Now onActivityResult() of my fragment is getting call. I want to know what this line super.onActivityResult() actually do when I added it in Activities onResult method ? in which order the onActivityResult() gets call, when activity has multiple fragments.

Mehtab Ahmed
  • 499
  • 3
  • 16
  • refer http://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – sasikumar Nov 04 '16 at 07:57
  • If you want to user host activity's onActivityResult then you have to start startActivityForResult() like this --> getActivity().startActivityForResult(....) – Sanwal Singh Nov 04 '16 at 07:58
  • No I dont want to use result method of host activity . I have independent result method in fragment that was not getting call and when i added super.onActivityResult() in result method of host activity then result method of fragment start working. I want to know why this happened. – Mehtab Ahmed Nov 04 '16 at 08:10
  • super.onActivityResult(requestCode, resultCode, data); this will call Main Frgament class "onActivityResult" method. * Receive the result from a previous call to * {@link #startActivityForResult(Intent, int)}. This follows the * related Activity API as described there in * {@link Activity#onActivityResult(int, int, Intent)}. – Shweta Chauhan Nov 04 '16 at 08:18

1 Answers1

3

I want to know what this line super.onActivityResult()

in your case, it calls the onActivityResult of FragmentActivity. There Android checks if there is a Fragment that should get its onActivityResult called.

in which order the onActivityResult() gets call, when activity has multiple fragments

The hosting Activity first and, eventually, the Fragment that called startActivityForResult.

For a deeper insight on the topic, have a look to FragmentActiviy#onActivityResult

Blackbelt
  • 156,034
  • 29
  • 297
  • 305