I have a DialogFragment which name is Dialog. How can I invoke it from a function which is out of activity and fragment in Android?
This is the DialogFragment:
class Dialog: DialogFragment() {
private var array = arrayOf("Yes", "No")
var a = ""
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.activity_main, container)
val myListView = rootView.findViewById(R.id.listview_1) as ListView
myListView.adapter = ArrayAdapter(context!!, R.layout.list_item, array)
myListView.setItemChecked(0,true)
val okbutton = rootView.findViewById<Button>(R.id.ok)
var cancelbutton = rootView.findViewById<Button>(R.id.cancel)
var title = rootView.findViewById<TextView>(R.id.title)
title.text="Choose one option"
cancelbutton.setOnClickListener { dismiss() }
okbutton.setOnClickListener {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show()
}
myListView.setOnItemClickListener { adapterView,
view,
position,
l
->
Toast.makeText(context, "${array[position]}", Toast.LENGTH_LONG).show()
}
return rootView
}
}
And this is function which I want to invoke Dialog from there:
private val fm = supportFragmentManager
fun TestFunction() {
Dialog().show(fm, "")
}
But supportFragmentManager is in red and is not recognized in the function.