带有导航组件的DialogFragment(方向问题)

时间:2020-05-13 05:35:50

标签: android android-architecture-navigation android-jetpack-navigation

导航组件2.3.0-aplha06

我在导航图中有一个对话框片段以及一些片段。

场景-快乐

片段A打开,单击按钮导航到DialogFragment 对话框片段模拟了一些进度,完成后将执行

findNavController().popBackStack()

然后片段A执行一些操作并导航到片段B。

场景-悲伤

片段A打开,单击按钮导航到DialogFragment 对话框片段模拟了一些进度 用户将设备的方向从纵向更改为横向

findNavController().popBackStack()

然后片段A执行一些操作并导航到片段B。

2020-05-13 10:53:32.436 15140-15140/com.demo.example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.demo.example, PID: 15140
    java.lang.IllegalStateException: Fragment ProgressDialogFragment{1765f4f} (7df114f6-88cf-4d24-9b07-54c0ff9edcb0)} not associated with a fragment manager.
        at androidx.fragment.app.Fragment.getParentFragmentManager(Fragment.java:945)
        at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:107)
        at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
        at com.demo.example.ProgressDialogFragment$PaymentTimer.onFinish(ProgressDialogFragment.kt:61)
        at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

ProgressDialogFragment代码

class ProgressDialogFragment : DialogFragment() {

private lateinit var txtProgressBar: DxpTextView

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return super.onCreateDialog(savedInstanceState).apply {
        window?.requestFeature(Window.FEATURE_NO_TITLE)
        window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        )
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        isCancelable = false
    }
}

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

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    txtProgressBar = view.findViewById(R.id.txt_progress_text)

    val timer = PaymentTimer(5000, 2500)
    timer.start()
}

inner class PaymentTimer(
    millisInFuture: Long,
    countDownInterval: Long
) : CountDownTimer(millisInFuture, countDownInterval) {

    override fun onFinish() {
        findNavController().previousBackStackEntry?.savedStateHandle?.set("progress", true)
        findNavController().popBackStack()
    }

    override fun onTick(millisUntilFinished: Long) {
        if (millisUntilFinished < 2500 && isAdded) {
            txtProgressBar.text = getString(R.string.progress_msg_2)
        }

    }
}}

0 个答案:

没有答案