简体   繁体   中英

How to open a DialogFragment written in Kotlin class from a Java class

I created a WaterQualityChecksDialogFragment, please correct if I have not written it correctly.

WaterQualityChecksDialogFragment.kt

class WaterQualityChecksDialogFragment : DialogFragment() {

@Inject
private lateinit var app: App

fun newTargetInstance(): WaterQualityChecksDialogFragment {
    val fragment = WaterQualityChecksDialogFragment()
    return fragment
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    app!!.bus.post(ScreenDimEvent(false))

    val builder = AlertDialog.Builder(activity!!.applicationContext)

    // Get the layout inflater
    val inflater = activity!!.layoutInflater

    @SuppressLint("InflateParams")
    val inflatedView = inflater.inflate(R.layout.dialog_water_quality_cheks, null)

    builder
            .setView(inflatedView)
            .setCancelable(false)
            /*
             * setPositiveButton and setNeutralButton are also set (and overridden) in {@link #onStart}
             * to stop Android automatically closing the dialog on click
             */
            .setPositiveButton(R.string.fuel_order_signature_dialog_save) { dialog, id ->

            }
            .setNeutralButton(R.string.fuel_order_signature_dialog_clear) { dialog, id ->

            }


    val dialog = builder.create()

    // Stop touch events outside the dialog from cancelling/dismissing
    dialog.setCanceledOnTouchOutside(false)

    return dialog
}

interface WaterQCChecksDialogListener {
    fun onDialogPositiveClick(dialog: WaterQualityChecksDialogFragment)

    fun onDialogNegativeClick(dialog: WaterQualityChecksDialogFragment)
}
}

ServiceOrderDialogHelper.java

public void showWaterQCChecksDioalog(){
    //Cannot call newTargetInstance() method.
    waterQualityChecksDialogFragment = WaterQualityChecksDialogFragment.newTargetInstance()
}

Put newTargetInstance() inside companion object to make this static like java

companion object {
    fun newTargetInstance(): WaterQualityChecksDialogFragment {
        val fragment = WaterQualityChecksDialogFragment()
        return fragment
    }
}

And call from java like below:

WaterQualityChecksDialogFragment.Companion.newTargetInstance();

Please check this code, it may helps you

 class DialogClassSample : DialogFragment() {

companion object {
    fun newInstance(): DialogClassSample {
        val dialog = DialogClassSample()
        return dialog
    }
}



override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    isCancelable = false
    return inflater.inflate(R.layout.view, container, false)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    setListener()
}

fun setListener() {
    button.setOnClickListener({
        dismiss()
    })
}

override fun onResume() {
    super.onResume()
    val window = dialog?.window
    val size = Point()
    val display = window?.windowManager?.defaultDisplay
    display?.getSize(size)
    val width = size.x
    window?.setLayout((width * 0.85).toInt(),    WindowManager.LayoutParams.WRAP_CONTENT)
    window?.setGravity(Gravity.CENTER)
  }
 }

and call fron your class

   val dialog = DialogClassSample.newInstance()//show dialog description according to user type
        dialog.show(activity?.supportFragmentManager?.beginTransaction(), DialogClassSample::class.java.name)

In on resume we have set dialog size dynamically ,because in some bigger size dialog its look not good so you can use as per your requiremnent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM