简体   繁体   English

应该在片段还是在活动上处理ActionBar?

[英]Should the ActionBar be handled on Fragments or on the Activity?

I am making a shopping list app, and I have two Fragments , a ProductListFragment and a ShoppingListFragment . 我正在制作一个购物清单应用程序,并且有两个Fragments ,一个ProductListFragmentShoppingListFragment I have ActionBar buttons to add elements to the List on ProductListFragment . 我有ActionBar按钮,可将元素添加到ProductListFragment上的List中。 My question is, should I handle these buttons on the Fragment or on the Activity? 我的问题是,我应该处理“片段”还是“活动”上的这些按钮?

You should allow your Activity to handle the events of your fragments. 您应该允许您的活动处理片段的事件。 This allows you flexibility in your design so that you can reuse your fragments in other Activities down the line. 这使您可以灵活地进行设计,以便可以在其他活动中重复使用片段。 Here is a great example of how to accomplish this using event callbacks. 是一个很好的示例,说明如何使用事件回调来完成此操作。 Good luck. 祝好运。

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

相关问题 在 Fragment 中使用 CoordinatorLayout 和 Activity 中的 ActionBar - Using CoordinatorLayout in fragments with ActionBar in Activity Android方向发生变化,片段活动处理了片段 - Android orientation change with fragments handled by fragment activity 使用ViewPager中的片段在Activity中的ActionBar中隐藏项目 - Hide item in ActionBar in Activity with Fragments in ViewPager 空指针异常,操作栏活动和片段 - Null pointer exception, actionbar activity and fragments 动作栏和片段? - Actionbar and Fragments? 如何在具有多个片段的Activity中在splitActionBar和默认ActionBar之间进行切换 - How to switch between splitActionBar and default ActionBar in Activity having multiple fragments ActionBar中的“后退”按钮隐藏在由Tabs中的“片段”启动的ListView活动中 - Back button in ActionBar gets hidden in the ListView activity started by Fragments in Tabs 应该在哪里创建片段? 在viewModel或Activity中? - Where fragments should be created? In viewModel or Activity? 活动暂停时是否应该销毁ViewPager片段? - Should a ViewPager fragments be destroyed when activity is paused? onActivityResult应该在哪里处理,对话片段,片段或活动? - Where should onActivityResult be handled, dialog fragment, fragment, or activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM