Espresso: Not able to launch inner fragment

Viewed 166

I am using launchFragmentInContainer<FragmentOne>() to launch fragment and able to perform actions using EspressoMatchers, it's all working fine.

But the issue comes when I want to launch another fragment onClick Of button(this code is inside FragmentOne) but while launching getting an error for FragmentTwo

Example:

onClick {
        val fragment = FragmentTwo.newInstance()
        val transaction = requireActivity().supportFragmentManager.beginTransaction()
        transaction.add(fragmentContainerID, fragment)
        transaction.commit()    
       }

Above code giving error java.lang.IllegalArgumentException: No view found for id 0x4d2 (unknown) for fragment FragmentTwo{bc39d06} (6f4636b5-f441-4e38-956a-47ac91d261bc) id=0x4d2}

How to resolve this inside fragment? or I have to do something in espresso test.

1 Answers

This issue is coming due to fragmentContainerID which should be unique and used under the hood of launchFragmentInContainer().

So, inside the espresso framework android.R.id.content is used as a fragment Id. This same id should be used while the fragment transaction.

Code Snippet:

var fragmentContainerID = android.R.id.content // It will do the trick and fragment will be added on top of stack
Related