The structure of the app is like this:
tabHost (in Activity) -> contains -> TabFragment(extend base container fragment)
1. Th
If you are using single activity and have fragments inside the NavHostFragment
, there is an issue of onActivityResult()
of the child fragment of NavHostFragment
not getting called.
To fix this issue, you need to call the onActivityResult()
of the child fragments manually from inside the onActivityResult()
of the host activity. The host activity is the activity that hosts your NavHostFragment
.
Here's the Kotlin code for onActivityResult()
of your host activity:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment)
val childFragments = navHostFragment?.childFragmentManager?.fragments
childFragments?.forEach { it.onActivityResult(requestCode, resultCode, data) }
}
In your Activity Override onActivityForResult() like this
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Now in your fragment u can get the activity result inside this
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("test1", "result2");
}
Make sure when your are calling start ActivityForResult in your frragment it should be like this
startActivityForResult(intent, REQUEST_CAMERA);
The reason why this doesn't work is because you are calling startActivityForResult()
from within a nested fragment. Android is smart enough to route the result back to an Activity
and even a Fragment
, but not to a nested Fragment
hence why you don't get the callback.
(more information to why that doesn't work here or on stackoverflow)
Now in order to make it work I suggest you manually route the callback to the ChildFragment
(=UploadType
) in the ParentFragment
(=BaseContainerFragment
):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment uploadType = getChildFragmentManager().findFragmentById(R.id.container_framelayout);
if (uploadType != null) {
uploadType.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
In my case, I've done by adding following code in my MainActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
TabActivity->ActivityA->FragmentB, it's not work.
use a bad bad bad way:
ActivityA.java
public void onSelectSomething(){
...
startActivityForResult(intent, 22222);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mFragment != null) {
mFragment.onActivityResult(requestCode, resultCode, data);
}
}
FragmentB.java
if(getActivity() instanceof ActivityA) {
((RepairerListActivity)getActivity()).onSelectSomething();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 22222) {
// do things
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Loop through all child fragments of the activity
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
// check if the current fragment is YourFragment
if (fragment instanceof YourFragment ) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}