关于Android中的BottomNavigationView的问题

huangapple go评论72阅读模式
英文:

Question about BottomNavigationView in Android

问题

我对BottomNavigationView有一些一般性的问题。我想在我的每个活动中都有一个BottomNavigationView,用于订购某种东西(例如食物)。它应该有4个按钮:

  • 返回
  • 信息
  • 统计
  • 我的订单

使用'返回'按钮,应用程序应该只返回到上一个活动。按钮'统计'和'我的订单'应该切换到一个持久活动,当不显示时不应销毁。'我的订单'应该显示最近的订单。按钮'信息'应该只显示关于当前项目或当前菜单的一些信息(取决于从哪个活动调用它)。所以基本上我有2个问题:

  1. '信息'、'统计'和'我的订单'活动应该是真正的活动还是只是片段?通常我认为至少'统计'和'我的订单'应该是真正的活动,因为它们是持久的。但在许多BottomNavigationView中,只使用片段?
  2. 如何将内容信息传递给'信息'活动/片段?这个活动/片段应该根据它被调用的活动显示信息。假设这些活动是不同的菜品。我是否必须为每道菜创建一个单独的信息活动/片段?或者我是否可以以某种方式定义一个动态的活动/片段,根据当前活动显示信息?

我很感谢每一个评论,也很感谢您的帮助。

英文:

I have general questions about BottomNavigationView. I would like to have a BottomNavigationView in each of my Activities in an App for ordering something (e.g. food). It should have 4 buttoms:

  • Back
  • Info
  • Stats
  • My Orders

With 'Back' the app should just go back to the previous activity. The buttoms 'Stats' and 'My Orders' should switch to a persistent activity that should not be destroyed when not being displayed. 'My Orders' should display the last orders. The buttom 'Info' should only display some information about the current item or current menu (depending from which activity it is called). So basically I have 2 questions:

  1. Should the Activities 'Info', 'Stats', and 'My Orders' be real Activities or just Fragments? Normally I think that at leat 'Stats', and 'My Orders' should be real Activities as they are persistent. But in many BottomNavigationView only Fragments are used?
  2. How can I pass content information to the Activity/Fragment 'Info'. This Activity/Fragment should display information based on the Activity is was called from. Let's say the Activities are different dishes. Do I have to create a separate Info-Activity/Fragment for each dish? Or can I somehow define a dynamic Activity/Fragment that displayes information based on the current Activity?

I'd appreciate every comment and I'd really appreciate your help.

答案1

得分: 1

  1. 推荐的方法是 Single ActivityMultiple fragments。你可以使用 Jetpack 的 Navigation 组件 来实现这一点。

  2. 如果需要将数据从一个 Activity/Fragment 传递给新的调用 Fragment,可以通过在调用 Fragment 上设置参数,然后在被调用的 Fragment 上获取参数来实现。如果有需要动态加载数据的情况,例如菜单 Fragment,可以创建一个单一的 Fragment 和通用布局,然后从后端动态加载数据。

设置参数的方法,可以参考这里:

https://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back/17436739#17436739

注意: 你可以在不使用 Navigation 组件 的情况下使用 Fragment,但需要自己使用 FragmentManagerFragmentTransaction 来管理,并且需要自行维护返回栈,这可能会相当复杂。

英文:
  1. The recommended approach is Single Activity and Multiple fragments.
    You can do this using Jetpack's Navigation Component
  2. In case you need to pass data from an Activity/Fragment to the new calling Fragment, it can be done by setting arguments on the calling fragment and then getting it on the called fragment. If there is something which requires to be dynamic, for example- dishes fragment, make a single fragment and common layout and load the data dynamically from the backend.

For Setting Arguments, this should help

https://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back/17436739#17436739

Note: You can use fragment without using Navigation Components but you have to use FragmentManager and FragmentTransaction and also have to maintain the Backstack by yourself which could be quite complicated

huangapple
  • 本文由 发表于 2020年8月11日 04:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63347230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定