Onactivityresult() Is Not Called When Startactivityresult() Is Called From Child Fragment In Viewpager
Solution 1:
In my case i was picking up the mobile number from the contact by using intent in my child fragment class i.e. one of tab of view pager.
calling the parent fragment from MainActivity:
// Home is my parent fragment which contains Tab Layout and View Pager. I am calling Home Fragment by specifying tag, i will use this tag later on.
Fragmentfragment=newHome();
fragmentManager.beginTransaction()
.replace(R.id.main_content, fragment, "MobileTag")
.addToBackStack(null)
.commit();
In My child Fragment class:
Intentintent=newIntent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
Now it will call the onActivityResult method of MainActivity class In Main Activity:
publicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
By Calling super in onActivityResult of MainActivity will make a call to Parent fragment's i.e. Home Fragment's onActivityResult method.
In Parent Fragment i.e. Home Fragment:
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
Fragmentfragment= getFragmentManager()
.findFragmentByTag("MobileTag")
.getChildFragmentManager()
.getFragments()
.get(0);
fragment.onActivityResult(requestCode, resultCode, data);
}
here I am getting the 0th Child of parent fragment e.g. In View Pager Adapter if Fragment are added like:
@Overridepublic Fragment getItem(int position) {
switch (position){
case0 :returnnewChildFragment0();
case1 : returnnewChildFragment1();
case2 : returnnewChildFragment2();
}
returnnull;
}
Then
getFragmentManager()
.findFragmentByTag("MobileTag")
.getChildFragmentManager()
.getFragments()
.get(0); // will return ChildFragment0
After this Make a call to child fragment's onActivityResult method:
And Finally In Child Fragment:
publicvoidonActivityResult(int reqCode, int resultCode, Intent data) {
Cursorcursor=null;
StringcontactNumber="";
Uriresult= data.getData();
// get the result here
}
Solution 2:
Currently, it's a known Android bug. Until this is solved a workaround can be used:
publicclassHackForResultFragmentextendsFragment {
privatestatic Fragment _callingFragment;
private Intent _intent;
privateint _requestCode;
publicstatic HackForResultFragment newInstance(int _requestCode, Intent _intent){
HackForResultFragmentfragment=newHackForResultFragment();
Bundlebundle=newBundle(2);
bundle.putInt("_requestCode", _requestCode);
bundle.putParcelable("_intent", _intent);
fragment.setArguments(bundle);
return fragment;
}
publicHackForResultFragment() {
}
publicvoidsetCallingFragment(Fragment callingFragment){
_callingFragment = callingFragment;
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundlearguments= getArguments();
if (arguments == null) {
//Error handling
LogUtil.Error("arguments should have been passed to HackForResultFragment");
}
else {
_requestCode = arguments.getInt("_requestCode");
_intent = arguments.getParcelable("_intent");
}
if (savedInstanceState == null) { //first time only
startActivityForResult(_intent, _requestCode);
}
}
@OverridepublicvoidonAttach(Context context) {
super.onAttach(context);
}
@OverridepublicvoidonActivityResult(finalint requestCode, finalint resultCode, final Intent data) {
if (_requestCode == requestCode) {
if (_callingFragment != null) {
_callingFragment.onActivityResult(requestCode, resultCode, data);
_callingFragment = null;
}
}
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
}
@OverridepublicvoidonSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("startActivityForResultAlreadyCalled",true);
}
}
In your calling fragment override startActivityForResult:
@OverridepublicvoidstartActivityForResult(final Intent _intent, finalint _requestCode) {
FragmentManagerfragmentManager= getActivity().getSupportFragmentManager();
HackForResultFragmentf= HackForResultFragment.newInstance(_requestCode,_intent);
f.setCallingFragment(this);
fragmentManager.beginTransaction().add(f, null).commit();
}
Solution 3:
This bug is now resolved in support library version 23.2.0, so the best way to solve this now is to upgrade the support library.
Source: https://code.google.com/p/android/issues/detail?id=40537#c40
Solution 4:
if FragmentActivity contain fragment1, fragment1 contain fragment2. Android can only handle fragment1.startActivityForResult
,fragment2.startActivityForResult
will not working.
For more detail and solution, watch the this answer answer
Post a Comment for "Onactivityresult() Is Not Called When Startactivityresult() Is Called From Child Fragment In Viewpager"