简体   繁体   English

kotlin android “OnbackPressed” 不覆盖任何内容

[英]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).我知道这已经问过几次了,但我还是什么都没得到(我在 android 开发中还很新)。

So i set up my back button in the MainActivity.kt like this:所以我在 MainActivity.kt 中设置了我的后退按钮,如下所示:

    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:我想要的是在某些片段中禁用了此后退按钮,因此我尝试在其中一个片段中覆盖onBackPressed() function (这是互联网上大多数人所说的):

    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 “OnBackPressed” 覆盖无

Am I missing something?我错过了什么吗? I'm already searching for the solutions but still confused over this.我已经在寻找解决方案,但仍然对此感到困惑。

Who knew... onSupportNavigateUp() works only on 4.0 and above.谁知道... onSupportNavigateUp() 仅适用于 4.0 及更高版本。 For below onNavigateUp() is called.对于下面的 onNavigateUp() 被调用。

so所以

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

        return navController.navigateUp()
    }

What you could do is set your nonbackbutton fragments to backstack when inserting them, which is done by您可以做的是在插入它们时将您的非后退按钮片段设置为 backstack,这是由

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) (如果您想要后退按钮功能,只需跳过 addtoBackStack 行)

Then, also in your activity, you override the onBackPressed() and check whether the backstack is empty or full.然后,同样在您的活动中,您覆盖 onBackPressed() 并检查 backstack 是空的还是满的。 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().然后,例如,您在支持后退按钮的片段上,只需执行 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().另一方面,如果它有东西,您可以执行navController.navigateUp()部分,完成后,使用 supportFragmentManager.popBackStackImmediate() 弹出它。

I think you could make something like this work, try it, and let us know:)我想你可以做这样的事情,试试看,然后告诉我们:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM