Skip to content

Instantly share code, notes, and snippets.

@kohendrix
Last active December 27, 2018 00:59
Show Gist options
  • Select an option

  • Save kohendrix/23ef5c59b45113fb93b5da29994f3c76 to your computer and use it in GitHub Desktop.

Select an option

Save kohendrix/23ef5c59b45113fb93b5da29994f3c76 to your computer and use it in GitHub Desktop.
Android Fragment that shows a dialog but not a DialogFragment
class NotDialogFragment : Fragment() {
private var reserveDismiss = false // resume時にこのdialogを閉じる
private var reserveShow = false // resume時に表示
private lateinit var dialogMain: LinearLayout
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater?.inflate(R.layout.fragment_load, container, false)?.apply {
dialogMain = findViewById<FrameLayout>(R.id.dialog_main_layout).also {
it.visibility = View.GONE
it.setOnTouchListener { _, _ -> true }
}
}
}
fun show() {
if (this.isResumed) {
activity.runOnUiThread {
dialogMain.visibility = View.VISIBLE
reserveShow = false
}
} else {
reserveShow()
}
}
fun dismiss() {
Log.d(TAG, "dismiss isResumed $isResumed")
if (this.isResumed) {
activity.runOnUiThread {
dialogMain.visibility = View.GONE
reserveDismiss = false
}
} else {
reserveDismiss()
}
}
override fun onResume() {
super.onResume()
if (reserveDismiss) this.dismiss()
else if (reserveShow) this.show()
}
private fun reserveDismiss() {
reserveDismiss = true
}
private fun reserveShow() {
reserveShow = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment