Home > Blockchain >  How to start an app by one fragment before the start destination?
How to start an app by one fragment before the start destination?

Time:11-15

I have a navigation code and it starts my app from titleFragment.

My MainActivity code can go to aboutFragment/rulesFragment from titleFragment. When I'm on about/rules fragment, I can click the up button and get back to titleFragment.

But what I want is when I click the up button when I'm on about/rules fragment, I go back to gameFragment.

I can change the app:startDestination so when I click up button it will direct me to gameFragment. But the problem is when restart the app, it will start from gameFragment, not titleFragment.

Any idea how to keep my app still starting from titleFragment but clicking the up button will direct me to gameFragment?

CodePudding user response:

It's weird thing what you asked but i think it's achievable with overriding back button click:

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                // Do your things in here like example below
                findNavController().navigate(R.id.action_aboutFragment_to_gameFragment)
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

You need to override on both about and rules fragment's buttons with related actions.

  • Related