Foxtrot 1-5
Foxtrot 1-5

Reputation: 379

kotlin android "OnbackPressed" overrides nothing

I know this already asked a few times, but I still don't get anything after all (I'm quite new in android development).

So i set up my back button in the MainActivity.kt like this:

    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

        val navController = this.findNavController(R.id.myNavHostFragment)

        NavigationUI.setupActionBarWithNavController(this, navController)

        supportActionBar?.setDisplayHomeAsUpEnabled(false)


    }


        // Set up the back button on action bar
        override fun onSupportNavigateUp(): Boolean {
            val navController = this.findNavController(R.id.myNavHostFragment)

            return navController.navigateUp()
        }
    }

What I want is that this back button is disabled in some fragments, so I tried to override the onBackPressed() function (It is what most people on the internet told) in one of the fragments:

    class DashboardFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Declare that this fragment has menu
        setHasOptionsMenu(true)


        // Set action bar title to "Main Dashboard"
        (activity as AppCompatActivity).supportActionBar?.title = "Main Dashboard"


        // Binding object for this fragment and the layout
        val binding: FragmentDashboardBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_dashboard, container, false)


        //Some codes here//


        return binding.root
    }

        // This is where the error occured
        override fun onBackPressed() {
            super.onBackPressed()
        }

        override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
            super.onCreateOptionsMenu(menu, inflater)
            inflater?.inflate(R.menu.nav_overflow_menu, menu)
        }
    }

But it returns an error saying:

"OnBackPressed" overrides Nothing

Am I missing something? I'm already searching for the solutions but still confused over this.

Upvotes: 1

Views: 1803

Answers (2)

victiMusPrime
victiMusPrime

Reputation: 53

What you could do is set your nonbackbutton fragments to backstack when inserting them, which is done by

val transaction = supportFragmentManager.beginTransaction()
transaction.addToBackStack(null)
transaction.replace(R.id.frame, fragment) //or add, whatever you use
transaction.commit()

(if you want back button functionality, just skip the addtoBackStack line)

Then, also in your activity, you override the onBackPressed() and check whether the backstack is empty or full. If it is empty, which is checked by

if(supportFragmentManager.backStackEntryCount() == 0)

then you are, for example, on a fragment that supports back button, and just do super.onBackPressed().

On the other hand, if it has something, you can do the navController.navigateUp() part, and when done, pop it using supportFragmentManager.popBackStackImmediate().

I think you could make something like this work, try it, and let us know :)

Upvotes: 0

AliReza Zahani
AliReza Zahani

Reputation: 21

Who knew... onSupportNavigateUp() works only on 4.0 and above. For below onNavigateUp() is called.

so

    override fun onNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.myNavHostFragment)

        return navController.navigateUp()
    }

Upvotes: 1

Related Questions