startActivityForResult() from a Fragment and finishing child Activity, doesn't call onActivityResult() in Fragment
Asked Answered
D

10

183

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.

Duumvir answered 13/6, 2013 at 11:16 Comment(2)
Try to put it (onActivityResult method) in the FirstActivity classAftonag
possible duplicate of onActivityResult not being called in FragmentNut
K
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);

Kleptomania answered 13/6, 2013 at 11:25 Comment(10)
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.Duumvir
Please replace getActivity().startActivityForResult(i, 1); with startActivityForResult(i, 1);Kleptomania
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).Townsley
@asQuirreL Yes. this is probably the reason.Wanderlust
@Townsley 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.Worldbeater
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.Hazard
in my case i had to use ((Activity) getContext()).startActivityForResult(intent, REQUEST_CODE);Tillio
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 pleaseAcervate
To note on this, always call the super in activity's onActivityResult and use fragment's startActivityForResult. May the force be with you.Gennie
This did not work for me as wellGargle
R
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! :)

Rearm answered 13/1, 2014 at 22:20 Comment(0)
H
31

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

Hinayana answered 8/8, 2016 at 3:55 Comment(1)
thanks mate ! but it requiere a Level 26 API .. so in my case I used getSupportFragmentManager().findFragmentByTag method but with your ideaConsuetudinary
W
15

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

Wanderlust answered 23/12, 2015 at 8:35 Comment(0)
D
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.

Deneb answered 14/10, 2017 at 15:11 Comment(2)
This is the best answer. The detail of defining the "launchMode" in the Manifest is vital.Dorton
This did not work for me, FirstActivity onActivityResult did not even executeGargle
F
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.

Flooring answered 17/2, 2017 at 6:48 Comment(0)
T
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) {

    }
}
Toddy answered 7/10, 2019 at 12:55 Comment(0)
I
1

just try this:

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

//just call 
startActivityForResult(intent, REQ_CODE);
//directly from fragment
Impendent answered 11/1, 2021 at 14:29 Comment(0)
S
0

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

Subtreasury answered 2/2, 2018 at 5:7 Comment(0)
M
-2

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

Monochrome answered 21/9, 2017 at 16:9 Comment(1)
Please explain this a bit further, curious to know what you were doingGargle

© 2022 - 2024 — McMap. All rights reserved.