[Solved] startActivityForResult() vs getActivity().startActivityForResult() in Android Fragment

What the differences between startActivityForResult() vs getActivity().startActivityForResult() in Android Fragment? And what is the behaviour differences in onActivityForResult() when called in Fragment?

Solution #1:

startActivityForResult() must handle it from the fragment‘s onActivityForResult()

getActivity().startActivityForResult() must handle it from the activity‘s onActivityForResult()

Basically:

If you’re on a fragment and you want to handle the result on the fragment, use onActivityForResult(), otherwise if you want to handle it from the activity of the fragment, use getActivity.startActivityForResult()

Respondent: Tenten Ponce
Solution #2:

startActivityForResult from fragment call startActivityForResult of its container Activity.
So for Fragment:-

Call startActivityForResult(Intent, int) from the fragment’s containing Activity.

Whereas when you use it in fragment directly some certain changes can be made on request code.And you will get modified request code in onActivityresult(), Cause i have faced some problem with modified request code recently . You can have a look into This Thread.

Respondent: ADM
Solution #3:

We can use startActivityForResult() inside Activity and getActivity().startActivityForResult() inside a Fragment.

Inshort, to get the Context of Activity we are preceeding it with getActiviy().

Instead, we can Call particular Fragment’s onActivityResult() as below from Actiivty.:

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

If we put it to more details then,

If You will call startActivityForResult() from your Fragment,the requestCode will be changed by the Activity that owns the Fragment.

So, If you want to get the correct resultCode in your activity you can try as below :

DO NOT USE : (in such case):

startActivityForResult(intent, 1);

USE :

getActivity().startActivityForResult(intent, 1);

Hope it helps..!

Respondent: Mashuk Khan
Solution #4:

No difference at all…
But A little bit difference is getActivity().

startActivityForResult() can only call from a fragment 🙂

Solution #5:

“startActivityForResult()” works on Activity classes.
when you are in Activity class, you can directly use this method.
when you are in fragment, because fragment creates on Activity, you should first get the activity that fragment based on it. means:

//in fragment class:
Activity activity = this.getActivity();

now you have the activity and i said, you can use “startActivityForResult()” on activity that you get from fragment.

activity.startActivityForResult();

hope helps.

Solution #6:

You have to make a call with reference to Activity, since startActivityForResult(Intent intent, int requestCode) is method specified in Activity.

So, if you call from YourActivity, it already has a reference of Activity like:

YourActivity extends AppCompatActivity (which is subclass of `Activity`)

But if you are calling it from YourFragment you need to specify Activity reference, which in this case will be the Activity containing YourFragment; hence we use getActivity() and then trigger startActivityForResult()

Respondent: Firoz Memon
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.