Android导航组件在Activity旋转时保留当前片段

时间:2019-07-23 11:50:37

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

我有一个带有导航组件的Activity Activity MVVM应用程序。当我导航时,一切正常。发生旋转时,我的应用崩溃了,于是我用了

findNavController().navigate(PermissionsFragmentDirections.Action_permissionsFragment_to_splashFragment())

,应用崩溃了。然后我更改为

findNavController().navigate(R.id.splashFragment)

现在这可行。奇怪,但这仅出现在onRequestPermissionsResult中。在其他地方,我就像在第一个代码示例中一样使用它。

我设法解决了这个问题,但是现在旋转屏幕时,我又回到了navigation_graph的开头。

对于导航,我的androidx.navigation.fragment.NavHostFragment中有activity_home.xml

所以我的问题是如何在Fragment轮换中保留当前的Activity

添加的代码

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

    Fabric.with(this, Crashlytics())

    binding = DataBindingUtil.setContentView(this, R.layout.activity_home)
    binding.viewModel = settingsViewModel
    binding.lifecycleOwner = this

    //small constraintlayout for showing state of app online/offline
    constraintLayoutStanje.setOnClickListener {
        settingsViewModel.progress()
        val id: String = getId(this)
        settingsViewModel.settings(id)
    }

    setObservers()

    //all time running task
    Handler().postDelayed({
        mHandlerTask.run()
    }, interval)

}

观察者

fun setObservers() {
    //status
    settingsViewModel.status.observe(this, Observer {
        when (it) {
            NacinDelaEnum.ONLINEINT -> this.constraintLayoutStanje.setBackgroundColor(
                ContextCompat.getColor(
                    this,
                    R.color.greenStat
                )
            )
            NacinDelaEnum.OFFLINE -> this.constraintLayoutStanje.setBackgroundColor(
                ContextCompat.getColor(
                    this,
                    R.color.redStat
                )
            )
            NacinDelaEnum.ONLINEEXT -> this.constraintLayoutStanje.setBackgroundColor(
                ContextCompat.getColor(
                    this,
                    R.color.yellowStat
                )
            )
        }
        settingsViewModel.doneProgress()
    })

    //Toast
    settingsViewModel.toastMessage.observe(this, Observer {
        if (it.isNotEmpty()) {
            toast(it)
            settingsViewModel.doneToast()
        }
    })


    //progressBar
    settingsViewModel.pb.observe(this, Observer {
        pb.visible(it)
    })

}

activity_home.xml中的NavHostFragment

<fragment
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true"
    android:id="@+id/mainFragment" app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/constraintLayoutStanje"
    app:layout_constraintHorizontal_bias="1.0" app:layout_constraintVertical_bias="1.0"/>

我的nav_graph.xml

enter image description here

提前谢谢

1 个答案:

答案 0 :(得分:2)

在清单文件中,

`<activity android:name=".MyActivity"
      android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
      android:label="@string/app_name">`

更多信息, aloneguid/parquet-dotnet