solaza
solaza

Reputation: 1291

Android Navigation component retain current Fragment on Activity rotation

I have single Activity MVVM application with navigation component. When I'm navigating everything works just fine. When rotation occur my app was crashing so then I used

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

and app crashed. Then I changed to

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

And now this works. Strange but this appear only in onRequestPermissionsResult. On other places I'm using it like on first code sample.

I managed to fix that but now when I rotate screen I'm back to start of my navigation_graph.

For navigation I have androidx.navigation.fragment.NavHostFragment inside my activity_home.xml

So my question is how to retain current Fragment on Activity rotation?

Added code

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)

}

Observers

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)
    })

}

NavHostFragment in activity_home.xml

<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"/>

My nav_graph.xml

enter image description here

Thank you in advance

Upvotes: 4

Views: 2815

Answers (1)

aspmm
aspmm

Reputation: 84

In your manifest file,

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

More Information, https://developer.android.com/guide/topics/resources/runtime-changes

Upvotes: 6

Related Questions