startActivityForResult() vs getActivity().startActivityForResult() in Android Fragment
Asked Answered
U

6

8

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

Unwary answered 14/12, 2017 at 6:53 Comment(2)
developer.android.com/reference/android/app/…, int, android.content.Intent) and developer.android.com/training/basics/intents/result.htmlHappen
Don't you know that there is a documentation for Android development?Inventory
M
4

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()

Mesquite answered 14/12, 2017 at 7:2 Comment(0)
G
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.

Guyer answered 14/12, 2017 at 7:7 Comment(0)
I
1

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..!

Iquique answered 14/12, 2017 at 6:55 Comment(4)
actually I used startActivityForResult() in fragment without using getActivity(). That's why I ask the behaviour differences.Unwary
See @Unwary There is no such difference among it. But, A little bit difference is we can use getActivity().startActivityForResult() only in fragment in order to get Context of Activity.Iquique
The whole point of a separate startActivityForResult method in Fragment is to avoid the need to go through all the Fragments in the Activity's onActivityResult. That's exactly the wrong thing to do.Intangible
Go through All Fragment's ?? Will you please explain it little more ?Iquique
T
0

No difference at all... But A little bit difference is getActivity().startActivityForResult() can only call from a fragment :)

Tolerance answered 14/12, 2017 at 7:2 Comment(0)
U
0

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()

Urethra answered 14/12, 2017 at 7:16 Comment(0)
P
0

"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.

Pedersen answered 14/12, 2017 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.