简体   繁体   English

使用导航组件处理工具栏后退按钮

[英]Handle Toolbar back button with Navigation component

I'm following single activity approach.我正在遵循单一活动方法。 I have navigation toolbar, whenever i go to other screens (fragments) instead of hamburger icon i will have back arrow.我有导航工具栏,每当我转到其他屏幕(片段)而不是汉堡包图标时,我都会有后退箭头。

What i want to achieve is, pop my current fragment using action on pressing toolbar back arrow.我想要实现的是,使用按下工具栏后退箭头的操作弹出我当前的片段。

I've tried我试过了

requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
    @Override
    public void handleOnBackPressed() {
        NavHostFragment.findNavController(EventDetailsFragment.this)
        .navigate(R.id.action_nav_event_details_to_nav_home);
    }
});

But not getting the call over there, i checked by running app in debug mode.但是没有接到那边的电话,我通过在调试模式下运行应用程序进行了检查。

in Activity oncreate:在创建Activity中:

navController = findNavController(R.id.my_nav_host)
//my_nav_host defined in activity xml file as id of fragment or FragmentContainerView
val appBarConfiguration = AppBarConfiguration(navController.graph)
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)

and:和:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        onBackPressed()
        return true
    }
    return true
}

then in your fragment:然后在你的片段中:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val callback: OnBackPressedCallback =
        object : OnBackPressedCallback(true /* enabled by default */) {
            override fun handleOnBackPressed() {
                //do what you want here
            }
        }
    requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}

Add this code in parent activity在父活动中添加此代码

Add in onCreate method在 onCreate 方法中添加

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Add this method also in parent activity也在父活动中添加此方法

 @Override
public boolean onSupportNavigateUp() {
    return super.onSupportNavigateUp();
}

If you are using custom toolbar in your xml, you may consider using below approach如果您在 xml 中使用custom toolbar ,您可以考虑使用以下方法

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_chat)

    setSupportActionBar(activity_chat_toolbar)

       
   activity_chat_toolbar.setNavigationOnClickListener {
        onBackPressed() <-- custom toolbar's back press
    }

    val navHostFrag =
            supportFragmentManager.findFragmentById(R.id.chat_fragment_container_view) as NavHostFragment
    navController = navHostFrag.navController
    navController.setGraph(R.navigation.chat_nav_graph, intent.extras)

    setupActionBarWithNavController(navController)
}

// Function to check startDestination to finish() the parent activity
override fun onBackPressed() {
    if(navController.graph.startDestination == navController.currentDestination?.id) {
        finish()
    } else {
        super.onBackPressed()
    }
}

override fun onSupportNavigateUp(): Boolean {
   return navController.navigateUp() || super.onSupportNavigateUp()
}

I'm following single activity approach.我正在遵循单一活动方法。 I have navigation toolbar, whenever i go to other screens (fragments) instead of hamburger icon i will have back arrow.我有导航工具栏,每当我 go 到其他屏幕(片段)而不是汉堡包图标时,我都会有后退箭头。

What i want to achieve is, pop my current fragment using action on pressing toolbar back arrow.我想要实现的是,使用按下工具栏后退箭头的动作弹出我当前的片段。

I've tried我试过了

requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
    @Override
    public void handleOnBackPressed() {
        NavHostFragment.findNavController(EventDetailsFragment.this)
        .navigate(R.id.action_nav_event_details_to_nav_home);
    }
});

But not getting the call over there, i checked by running app in debug mode.但是没有接到电话,我通过在调试模式下运行应用程序进行了检查。

Toolbar toolbar = findviewbyid(R.id.toolbar);
toolbar.setnavigationonclicklistener(new view.onclicklistener(){
 code here  
});
setsupportactionbar(toolbar);

暂无
暂无

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

相关问题 使用工具栏处理导航组件中的物理后退按钮 - Handle physical back button in Navigation Component with Toolbar Android 架构组件导航:工具栏后退按钮丢失,后退不起作用 - Android architecture component navigation: toolbar back button missing, back not working 使用导航组件时如何覆盖工具栏中后退按钮的行为 - How to override the behavior of back button in toolbar when using navigation component 处理导航后退按钮 - Handle Navigation Back Button 如果我使用导航组件在每个片段中设置工具栏,如何删除顶级片段工具栏中的后退按钮? - how to remove back button in the toolbar of top level fragment if I set toolbar in each fragment using navigation component? 使用导航组件和向上导航时如何返回上一个活动(工具栏上的后退按钮) - How to return to previous activity when using Navigation Component and Up navigation (back button on toolbar) 使用导航视图和工具栏不显示后退按钮 - Back button not displaying using navigation view and toolbar 如何使用导航组件隐藏图标并仅在工具栏中显示后退按钮? - How to hide the icons and only show the back button in the toolbar using navigation component? 按钮返回带有导航组件的 startDestination - button back in startDestination with navigation component Android - 在处理带片段的导航时更新活动中的工具栏标题 - Android - update toolbar title in activity while handle back navigation with fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM