someonetonobody
someonetonobody

Reputation: 77

adding a child fragment in my bottomsheet fragment in Kotlin

I am trying to reuse already created fragment in my bottom sheet and for this I am using below code.

class SummaryBottomSheet: BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val bundle = Bundle()
        val diskOnly =false;

        val ft: androidx.fragment.app.FragmentTransaction = childFragmentManager.beginTransaction()
        ft.replace(R.id.reader_placeholder, ReaderFragment())
        ReaderFragment().arguments = Bundle().apply {
            bundle.putBoolean("diskonly",false)
        }
        ft.commit()
    }
}

now I am sending arguments in bundle to the fragment which I want to have in bootomsheet fragment. and this fragment also recieves arguments from navigator navArgs() but that is not happening in my bottomsheet as now I am not navigating rather using same fragment in bottomsheet.

fragment which I am reusing is:

lass ReaderFragment : BaseFragment(R.layout.fragment_reader), PageletActionMediator {

    private val args: ReaderFragmentArgs by navArgs()
    private val viewModel: ReaderViewModel by viewModels()
    private val activityViewModel: MainActivityViewModel by activityViewModels()

    private lateinit var recyclerView: RecyclerView
    private lateinit var viewAdapter: PageletListAdapter
    private lateinit var viewManager: LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        Timber.d("created ")


        val arguments = requireArguments()
        val diskOnly = arguments.getBoolean("diskonly")
        shortAnimationDuration = resources.getInteger(android.R.integer.config_shortAnimTime)
 
if(arguments){
        slug?.let { slug ->
            viewModel.initWithSlug(slug, diskOnly)
            return
        }}
else args?.let{
slug ->
            viewModel.initWithSlug(slug, diskOnly)
            return
}
    }

My app is crashing everytime with the error:

java.lang.IllegalStateException: Fragment ReaderFragment{5b3fc12} (7d5de6e0-3258-468c-8db4-d201b4c3f672) id=0x7f090346} does not have any arguments.
                         E      at androidx.fragment.app.Fragment.requireArguments(Fragment.java:679)
                         E      at com.pratilipi.comics.ui.reader.ReaderFragment.onCreate(ReaderFragment.kt:91)
                         E      at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
    ```


Upvotes: 0

Views: 1198

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199880

You're not setting the arguments on the fragment you actually use in your replace call:

// Here you create a fragment add use it in replace()
ft.replace(R.id.reader_placeholder, ReaderFragment())

// Here you create a *second* fragment, set its arguments,
// then do nothing with it.
ReaderFragment().arguments = Bundle().apply {
    bundle.putBoolean("diskonly",false)
}

Instead, you need to set your arguments on the fragment you use with replace:

val fragment = ReaderFragment()
fragment.arguments = Bundle().apply {
    bundle.putBoolean("diskonly",false)
}
ft.replace(R.id.reader_placeholder, fragment)

Upvotes: 1

Related Questions