1

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.

Abed Murad
  • 322
  • 1
  • 12
dimas hermanto
  • 369
  • 7
  • 22
  • `onBackPressed()` is a function in `Activity`, not in `Fragment`. So, you cannot override it in your `DashboardFragment`. – CommonsWare May 13 '20 at 18:19
  • Any idea how i must override them then ? – dimas hermanto May 13 '20 at 18:21
  • 1
    I would strongly recommend to not disable the BACK button. But, if you do, you would need to override `onBackPressed()` in your activity and make decisions there. – CommonsWare May 13 '20 at 18:22
  • 1
    @CommonsWare - that's not the case anymore, you'd want to follow the [Provide custom back navigation documentation](https://developer.android.com/guide/navigation/navigation-custom-back), which is what allows any component (including a fragment) to intercept and handle the back button. – ianhanniballake May 13 '20 at 18:45
  • @ianhanniballake: Ah, sorry, I forgot that Navigation provided a hook for that. Thanks! – CommonsWare May 13 '20 at 18:48
  • 1
    @CommonsWare - technically, it is part of AndroidX Activity, which sits even below Fragments. Just happens that higher level libraries such as Fragments and Navigation hook into it as well. – ianhanniballake May 13 '20 at 19:13
  • https://stackoverflow.com/questions/60462748/not-working-onbackpressed-when-setcancelable-of-alertdialog-is-false – MMG May 13 '20 at 19:52

2 Answers2

1

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

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

victiMusPrime
  • 53
  • 2
  • 9