Search code examples
javaandroidandroid-fragmentskotlinandroid-framelayout

Kotlin Display Fragment inside DialogFragment with FrameLayout


Im creating a DialogFragment but inside that Fragment there's a FrameLayout that is gonna change between fragments dynamically.

To have a better control over all the fragments i create an instance class to handle the fragments named NavigationDelegate

class NavigationDelegate(private val fragmentManager: FragmentManager, private val containerId: Int) {
    init {
        fragmentManager.addOnBackStackChangedListener {
            val currentFragment = fragmentManager.findFragmentById(containerId)
            currentFragment?.userVisibleHint = true
        }
    }

    fun navigateTo(to: Fragment, addToBackStack: Boolean = true){
        val transaction = fragmentManager.beginTransaction().replace(containerId, to)
        if(addToBackStack) transaction.addToBackStack(null)
        transaction.commit()
    }

    fun navigate(from: Fragment? = null, to: Fragment, addToBackStack: Boolean = true) {
        from?.let {
            from.userVisibleHint = false
            val transaction = fragmentManager.beginTransaction().replace(containerId, to)
            if(addToBackStack) transaction.addToBackStack(null)
            transaction.commit()
        }.let {
            val transaction = fragmentManager.beginTransaction().add(containerId, to)
            if(addToBackStack) transaction.addToBackStack(null)
            transaction.commit()
        }
    }
}

This is the Section where i call the DialogFragment to be created.

val purchase = inputFragmentView!!.findViewById<MaterialButton>(R.id.purchaseButton)
        purchase.setOnClickListener {
            val paymentSteps: PaymentStepsFragment?
            paymentSteps = PaymentStepsFragment()
                paymentSteps.isCancelable = false
                if (!paymentSteps.isVisible) paymentSteps.show(fragmentManager, "paymentSteps")
        }

And here is the PaymentStepsFragment

class PaymentStepsFragment : DialogFragment() {

    private var viewer: View? = null
    lateinit var fragmentContainer: FrameLayout
    lateinit var navigationDelegate: NavigationDelegate

    override fun getTheme(): Int {
        return R.style.SlideUpDownDialog
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        when {
            viewer != null -> (viewer!!.parent as ViewGroup).removeView(viewer)
        }
        viewer = inflater.inflate(R.layout.fragment_steps_payment, container, false)

        dialog?.window?.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        return viewer
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val imageStep = view.imageSteps
        imageStep.setSteps(R.drawable.ic_wallet_step_1, R.drawable.ic_deliver_step_2, R.drawable.ic_review_step_3)
        imageStep.scaleUp = 2.0f
        imageStep.animationDuration = 500
        view.next.setOnClickListener {
            imageStep.next()
        }
        view.back.setOnClickListener {
            imageStep.previous()
        }
        fragmentContainer = view.framePayment
        navigationDelegate = NavigationDelegate(fragmentManager!!, fragmentContainer.id)
        navigate(to = PaymentFragment(), addToBackStack = false)
    }

    override fun onResume() {
        super.onResume()
        val decorView = dialog.window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        decorView.systemUiVisibility = uiOptions
    }

    private fun navigate(from: Fragment? = null, to: Fragment, addToBackStack: Boolean = true) {
        from?.let {
            navigationDelegate.navigate(from, to, addToBackStack)
        }.let {
            navigationDelegate.navigateTo(to, addToBackStack)
        }
    }
}

and when i display the DialogFragment i got this error message:

java.lang.IllegalArgumentException: No view found for id 0x7f08008f (ni.devotion.hisabapp:id/framePayment) for fragment PaymentFragment{7b58f52 #3 id=0x7f08008f}

but the view exists in the layout, please anyone know how to solve this issue...

<FrameLayout
            android:id="@+id/framePayment"
            android:layout_width="match_parent"
            android:layout_height="300dp"/>

Solution

  • To solve the problem i change:

    navigationDelegate = NavigationDelegate(fragmentManager!!, fragmentContainer.id)
    

    into

    navigationDelegate = NavigationDelegate(this.childFragmentManager, fragmentContainer.id)