带活动的NavigationDrawer与带片段的NavigationDrawer [英] NavigationDrawer with Activities vs. NavigationDrawer with Fragments

查看:110
本文介绍了带活动的NavigationDrawer与带片段的NavigationDrawer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,以我当前正在使用的应用程序为例: -它具有包含多个项目的navigationDrawer;目前我有两个项目感兴趣,我将它们称为X和Y.

Take for example the app I'm currently working on: - it has an navigationDrawer with multiple items; there are two items which interest me for now, i'll call them X and Y.

  • 单击X和Y时,显示包含x元素或y元素列表的片段

  • both X and Y, when clicked on, display a fragment containing a list of x-elements or y-elements

selecting和x或y列表元素显示一个新的片段,在其中我显示有关选择项的信息; x和y元素的视图片段不同

selecting and x or y list element displays a new fragment in which I display info about the select item; the view fragments are different for x and y elements

在视图片段中,我可以选择编辑显示编辑片段的特定元素

in the view fragment I can choose to edit the specific element which brings up an edit fragment

片段方法有效,但是我花了一些时间来管理片段之间的导航.另外,我可能必须在抽屉中添加一些与X和Y相似的新项目.我的主要活动(其中有抽屉并进行片段切换)已经非常密集,这使我想到了一个问题:我从片段切换到活动?我正在考虑选择一个抽屉项目时开始一个新活动,并处理与该活动中所选项目相关的列表/视图/编辑片段,而不是在单个活动中处理所有项目的所有片段.

The fragment approach is working, but it took me a while to manage the navigation between the fragments. Also, I will probably have to add some new items in the drawer similar with X and Y. My main activity, in which I have the drawer and I do the fragment switching, is quite dense already, which brings me to my question: should I switch from fragments to activities? I was thinking about starting a new activity when a drawer item is selected and handle the list/view/edit fragments related to the selected item in that activity, rather than handling all fragments for all items in a single activity.

这是个好主意吗?设计不好吗?

Is it a good idea? Is it bad design?

推荐答案

我在类似的船上,并且使用了Activity方法,这样,对于每个特定的导航,我都将每个Activity带有片段组,然后单击NavigationView.显然我使用了NavigationView,但是仅用一个MainActivity管理所有这些片段确实是一项艰巨的任务.

I was in similar boat and I used Activities method, that way I have each Activity with group of fragments for a particular nav click on the NavigationView. Obviously I use NavigationView, but managing all those fragments with just one MainActivity is really a painful task.

当我们单击导航项目时,我更喜欢由EachActivity管理自己的片段.这使我获得了更好的性能,因为我不必担心太多片段的生命周期,它是backStack并添加/删除/显示/隐藏地狱.

I rather prefer EachActivity managing their own fragments when we click a nav item. This gives me a better performance, since I do not need to worry about lifecycle of soo many fragments and it's backStack and add/remove/show/hide hell.

我使用以下SO问题巧妙地使用了实现NavigationDrawer的BaseActivity,并将其与所有其他Activity共享.真正的魔力是它不重复代码,或者不只是普通的旧继承技术.

I used the following SO Question to cleverly use a BaseActivity implementing NavigationDrawer, and sharing it with all the other Activities. The real magic is it does not duplicate code, or it's not just plain old Inheritance technique.

这是链接,请检查一下

我在两个项目中都使用了这种方法,它运行得很好,而且我从一开始就不必处理片段管理.

I used this method in two of my project's and it's running very well plus I do not have to deal with fragment management right from the very start.

这篇关于带活动的NavigationDrawer与带片段的NavigationDrawer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆