更改输入/退出转换后DialogFragment不再持久

时间:2019-05-21 14:50:48

标签: android kotlin android-dialogfragment

我正在实现一个对话框片段,该片段具有自定义过渡(从屏幕底部进入和存在)。我现在遇到的问题是,当用户将我的应用程序放在后台然后恢复它时,过渡将重新开始,而我希望当用户在不进行过渡的情况下重新输入应用程序时将显示对话框。

到目前为止,这是我的代码:

MyFragmentDialog

class MyFragmentDialog : BaseDialogFragment() {

override fun getDialogLayout(): Int {
    return R.layout.episode_dialog_layout
}

override fun setUp() {
}

override fun setupDialog(bundle: Bundle?) {

}

override fun getFragmentDialogName(): String {
    return ""
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NO_TITLE, R.style.MyDialogTheme)
    retainInstance = true

}

override fun onDestroyView() {
    val dialog = dialog
    // handles https://code.google.com/p/android/issues/detail?id=17423
    if (dialog != null && retainInstance) {
        dialog.setDismissMessage(null)
    }
    super.onDestroyView()
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    dialog!!.window!!.attributes.windowAnimations = R.style.MyDialogTheme
    val width = WindowManager.LayoutParams.MATCH_PARENT
    val height = WindowManager.LayoutParams.WRAP_CONTENT
    dialog!!.window!!.setLayout(width, height)
    dialog!!.window!!.setGravity(Gravity.BOTTOM)
}

override fun onResume() {
    super.onResume()

}

companion object {

    fun newInstance(bundle: Bundle?): MyFragmentDialog {

        val dialog = MyFragmentDialog()
        dialog.arguments = bundle
        return dialog 
    }

}

}

BaseDialogFragment

abstract class BaseDialogFragment : DialogFragment()
{
       override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(getDialogLayout(),container,false)

        setUp()
        setupDialog(arguments)


        return view

    }

    protected abstract fun getDialogLayout() : Int

    protected abstract fun setUp()

    protected abstract fun setupDialog(bundle : Bundle?)

    abstract fun getFragmentDialogName() : String

}

styles.xml

<style name="MyDialogTheme" parent="android:Theme.Dialog">

        <!-- No backgrounds, titles or window float -->
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>


    </style>

    <style name="MyAnimation.Window" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_bottom</item>
    </style>

0 个答案:

没有答案