What the differences between startActivityForResult()
vs getActivity().startActivityForResult()
in Android Fragment? And what is the behaviour differences in onActivityForResult()
when called in Fragment?
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()
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.
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..!
startActivityForResult()
in fragment without using getActivity()
. That's why I ask the behaviour differences. –
Unwary 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 No difference at all... But A little bit difference is getActivity().startActivityForResult() can only call from a fragment :)
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()
"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.
© 2022 - 2024 — McMap. All rights reserved.