182

FirstActivity.Java has a FragmentA.Java which calls startActivityForResult(). SecondActivity.Java call finish() but onActivityResult never get called which is written in FragmentA.Java.

FragmentA.Java code:

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // some code
    Intent i = new Intent(getActivity(), SecondActivity.class);
    i.putExtra("helloString", helloString);
    getActivity().startActivityForResult(i, 1);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    getActivity();
    if(requestCode == 1 && resultCode == Activity.RESULT_OK) {
       //some code
    }
  }

SecondActivity.Java code:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //some code
       Intent returnIntent = new Intent();
       returnIntent.putExtra("result", result);                          

       setResult(Activity.RESULT_OK,returnIntent);     
       finish();
  }

I have tried debugging the code, but onAcitvityResult() never get called.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
mrsus
  • 5,446
  • 4
  • 31
  • 44
  • 1
    Try to put it (onActivityResult method) in the FirstActivity class – viplezer Jun 13 '13 at 11:25
  • 2
    possible duplicate of [onActivityResult not being called in Fragment](http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment) – QED Nov 15 '13 at 04:02

10 Answers10

291

You must write onActivityResult() in your FirstActivity.Java as follows

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

So this will call your fragment's onActivityResult()

Edit: the solution is to replace getActivity().startActivityForResult(i, 1); with startActivityForResult(i, 1);

mrsus
  • 5,446
  • 4
  • 31
  • 44
Kevin Adesara
  • 3,830
  • 1
  • 17
  • 18
  • 5
    Thanks @Kevin , i tried what you said but it's not working. But i debugged the code, i see `onActivityresult()` of `FirstAcivity.Java` gets called and `onActivityresult()` of `FragmentA.Java` never get called. Please help. – mrsus Jun 13 '13 at 11:46
  • 54
    Please replace getActivity().startActivityForResult(i, 1); with startActivityForResult(i, 1); – Kevin Adesara Jun 13 '13 at 12:01
  • 17
    Could it be that the answer in the accepted solution is actually not what is wrong here, and the *real* solution is to just use `startActivityForResult` instead of `getActivity().start...`? Because as far as I can see, you are overriding the definition of `onActivityResult` with its own definition, in the Activity (i.e. this behaves exactly the same as if the override in the answer did not exist). – amnn Jul 17 '14 at 09:27
  • @asQuirreL Yes. this is probably the reason. – android developer Aug 17 '14 at 11:24
  • @amnn Actually, BOTH of those are issues. I had already removed my `getActivity()` but still wasn't getting the result in the fragment until I added the `super` call to my Activity. – ashishduh Jun 03 '16 at 21:56
  • As long as the only thing you are doing in the you are doing is calling the super method, you don't even need to override it. Your solution works though. – prompteus Jan 04 '17 at 22:10
  • in my case i had to use `((Activity) getContext()).startActivityForResult(intent, REQUEST_CODE);` – Saul_programa Jul 02 '18 at 09:31
  • 1
    I do the same as you explain, override onActivityResult() in activity also in fragment with super in both and make startActivityForResult (intent ,i), but did not work, I need help please – Amal Kronz Jul 24 '18 at 05:38
  • To note on this, always call the super in activity's onActivityResult and use fragment's startActivityForResult. May the force be with you. – ralphgabb Nov 23 '18 at 01:19
34

Kevin's answer works but It makes it hard to play with the data using that solution.

Best solution is don't start startActivityForResult() on activity level.

in your case don't call getActivity().startActivityForResult(i, 1);

Instead, just use startActivityForResult() and it will work perfectly fine! :)

Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
Amit Patel
  • 1,795
  • 16
  • 20
30

You must write onActivityResult() in your FirstActivity.Java as follows

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

This will trigger onActivityResult method of fragments on FirstActivity.java

Sagar Kacha
  • 8,338
  • 4
  • 18
  • 27
Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28
15

The fragment already has startActivityForResult, which would call onActivityResult in the fragment if you use it, instead of getActivity()...

Alex M
  • 2,756
  • 7
  • 29
  • 35
android developer
  • 114,585
  • 152
  • 739
  • 1,270
10

The most important thing, that all are missing here is... The launchMode of FirstActivity must be singleTop. If it is singleInstance, the onActivityResult in FragmentA will be called just after calling the startActivityForResult method. So, It will not wait for calling of the finish() method in SecondActivity.

So go through the following steps, It will definitely work as it worked for me too after a long research.

In AndroidManifest.xml file, make launchMode of FirstActivity.Java as singleTop.

<activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_main"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar" />

In FirstActivity.java, override onActivityResult method. As this will call the onActivityResult of FragmentA.

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

In FragmentA.Java, override onActivityResult method

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("FragmentA.java","onActivityResult called");
}

Call startActivityForResult(intent, HOMEWORK_POST_ACTIVITY); from FragmentA.Java

Call finish(); method in SecondActivity.java

Hope this will work.

Md Shahbaz Ahmad
  • 345
  • 4
  • 12
6

We could call startActivityForResult() directly from Fragment So You should call this.startActivityForResult(i, 1); instead of getActivity().startActivityForResult(i, 1);

Intent i = new Intent(getActivity(), SecondActivity.class);
i.putExtra("helloString", helloString);
this.startActivityForResult(i, 1);

Activity will send the Activity Result to your Fragment.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
sanjaymith
  • 216
  • 3
  • 7
2

May it's late for the answer. But will be helpful for anyone.

In My case want to call activity from Fragment and setResult back from the fragment.

I have used getContext of Fragment Like.

startActivityForResult(new Intent(getContext(), NextActivity.class), 111);

And Set Result from Fragment

getActivity().setResult(Activity.RESULT_OK);
                    getActivity().finish();

Now Getting Result to Fragment with

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode == 111) {

    }
}
ajay singh
  • 169
  • 1
  • 5
1

just try this:

//don't call getActivity()
getActivity().startActivityForResult(intent, REQ_CODE);

//just call 
startActivityForResult(intent, REQ_CODE);
//directly from fragment
0

onActivityResult() of MAinActivity will call , onActivityResult() of Fragement wont call because fragment is placed in Activity so obviously it come back to MainActivity

dileep krishnan
  • 326
  • 4
  • 7
-2

Don't call finish() in onCreate() method then it works fine.

Alex M
  • 2,756
  • 7
  • 29
  • 35