Using FragmentManager In RecyclerView Adapter Click Event
With the deprecation of FragmentManager
, Android Studio desn't provide any code suggestions for the deprecated code.
The problem is fragmentManager
in val manager = (holder.itemView.context as Activity).fragmentManager
as it returns this error:
'getter for fragmentManager: FragmentManager!' is deprecated. Deprectaed in Java
I'm already using import androidx.fragment.app.DialogFragment
but the issuee still doesn't go away. Also, I've already seen this question, but it's not clear on what should be used instead in Kotlin.
holder.myButton.setOnClickListener {
val dialog = MyDialogFragment()
val manager = (holder.itemView.context as Activity).fragmentManager
dialog.show(manager, "example")
}
Answer
According to the documentation, you should get the support manager instead. You'll just have to cast the context
to FragmentActivity
instead of Activity
, like this:
val manager = (holder.itemView.context as FragmentActivity).supportFragmentManager
EDIT: Make sure that MyDialogFragment
extends androidx.fragment.app.DialogFragment
, not android.app.DialogFragment
. Your Activity should also extend AppCompatActivity
(or at least FragmentActivity
) to make it work correctly.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View