Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult not call in the Fragment

The structure of the app is like this:

tabHost (in Activity) -> contains -> TabFragment(extend base container fragment)

1. The code in Activity:

tabHost.addTab(
                tabHost.newTabSpec("home").setIndicator("",
                        getResources().getDrawable(R.drawable.btn_home)),
                HomeFragment.class, null);

2. The code in HomeFragment (Notice that HomeFragment is not the actual function but a container like this, and it extend BaseContainerFragment):

public class HomeFragment extends BaseContainerFragment {

    public Home homeFrag;
    private boolean mIsViewInited;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.container_fragment, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!mIsViewInited) {
            mIsViewInited = true;
            initView();
        }
    }

    private void initView() {
        homeFrag = new Home();
        replaceFragment(homeFrag, false);
    }

}

3. BaseContainerFragment

public class BaseContainerFragment extends Fragment {

    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(R.id.container_framelayout, fragment);
        transaction.commit();
    }

    public boolean popFragment() {
        boolean isPop = false;
        if (getChildFragmentManager().getBackStackEntryCount() > 0) {
            isPop = true;
            getChildFragmentManager().popBackStack();
        }
        return isPop;
    }

}

4. In the Home (The actual content of the fragment)

UploadType fragment = new UploadType();
                    Bundle bundle = new Bundle();
                    bundle.putString("form_type", "request");
                    fragment.setArguments(bundle);
                    ((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);

5. And in the UploadType , I call the camera activity but onActivityResult is only return in the main activity.

startActivityForResult(intent, REQUEST_CAMERA);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("test1", "result2");
        super.onActivityResult(requestCode, resultCode, data);
}

How can I trigger the onActivityResult at UploadType? Thanks for help.

like image 231
user782104 Avatar asked Feb 13 '15 09:02

user782104


People also ask

How to call onactivityresult inside a fragment in Android Studio?

you can call onActivityResult inside a Fragment in android studio 3.5 with ease, first, there should be an activity where you are coming to get result. OnActivity result means it has to give a resultant view when prompted. Now in the previous activity lets say

Why onactivityresult of nested fragment can't be called?

And it's all done. Activity could send the result to only the Fragment that has been attached directly to Activity but not the nested one. That's the reason why onActivityResult of nested fragment would never been called no matter what. /** Pass your fragment reference **/ frag.startActivityForResult (intent, REQUEST_CODE); // REQUEST_CODE = 12345

Is it possible to get activity result from a fragment?

Definitely it will work, It will work same like in activities. You have call startActivityForResult (intent, requestCode); and normally get result in if you call startActivityForResult () in fragment , result is delivered to parent activity.

Why does startactivityforresult() call Super For unhandled result codes?

The hosting activity overrides onActivityResult (), but it did not make a call to super.onActivityResult () for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult () call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments.


2 Answers

For NavHostFragment of Navigation Architecture Component

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) }
}
like image 118
Yogesh Umesh Vaity Avatar answered Nov 16 '22 01:11

Yogesh Umesh Vaity


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);
}
like image 33
Jeroen Mols Avatar answered Nov 16 '22 03:11

Jeroen Mols