为什么不经常使用的片段与一个活动 [英] Why not always use fragments with one activity

查看:173
本文介绍了为什么不经常使用的片段与一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是一个活动有许多碎片为什么会有人创建活动?我看到,随着碎片,它更容易共享数据和加上有更好的使用的onPause的,以确定是否该应用去背景或片段被切换。

Why would someone creates activities as opposed to one activity with many fragments? I see that with fragments, it is easier to share data and plus you have better use of onpause to determine if the app went to background or fragment is being switched.

我缺少的东西?

感谢

推荐答案

(道歉提前较长的答案)

(apologies for the long answer in advance)

我觉得没有什么是从这样做,阻止你。

I don't think anything is stopping you from doing that.

说了这么多,我怎么看活动,是作为一个地方,我可以定义当前过渡整个UI的屏幕应该怎么看:它裂解它应该包括以及如何将这些片断会坐在并排侧多重要的是应用程序的流程是如何工作的。

Having said that, How I look at activities is as a place where I can define how the entire UI screen for the current transition should look: which fragments it should include and how these fragments should sit side-by-side and more importantly how the application flow works.

在我的活动片段应一起组成与功能我期待有一个有意义的画面,应该是可以重复使用的,但他们不应该知道的其他片段presence。这是活动的作业。事实上,我强烈地由一个片段不应该知道的另一个片段的存在(虽然现在你可以包括内部片段的片段哪一种失败这一点)的规则遵守。如果我需要过渡到完全不同的使用情况,我preFER用活动来处理片段使用的过渡和新的布局和设置。

Fragments within my activity should together make a meaningful screen with functionalities I expect and should be re-usable but they shouldn't know of other fragments presence. That's the job of the activity. In fact, I strongly abide by the rule that a fragment should never know of another fragment's existence (although nowadays you can include fragments inside fragments which kind of defeats this point). If I need to transition to a completely different set of use-cases, I prefer to use activities to handle the transition and the new layout and sets of fragments to use.

我必须这样做?不,我可以做到这一切在一个主要活动,做这些准活性片段之间的转换自己,但我为什么要这么做时,有已经建立了一个框架,它会为我吗?使用意图初始化活动(过渡)是很多简单和清晰,实现比手动写我自己的引擎,跟踪我的所有片段的状态,并决定哪些启动,哪些摆脱。

Do I have to do it? No. I can do it all in one main activity and do the transitions between these "quasi-activity fragments" myself but why would I want to do that when there's already a framework in place that does it for me? Using intents to initialize activities (to transition) is a lot simpler and cleaner to achieve than manually writing my own engine that keeps track of the state of all my fragments and decides which ones to initiate and which ones to get rid of.

同样,处理不同的取向和器件尺寸更易于使用的活动内置的功能比自己动手做,但再一次,没有什么是这样做阻止你。

Similarly, handling different orientations and device sizes is easier to do using built-in functionalities in activities than doing it yourself but again, nothing is stopping you from doing so.

更容易解释这一个例子:

Much easier to explain this in an example:

成像这种用例:我需要编写一个应用程序,显示歌曲和电影列表。当我点击的歌曲之一,它应该打开媒体播放器,并在单独的屏幕上玩,当我点击一个电影,它应该打开一个视频播放器和播放视频。有明确的过渡位置:从主列表,音频播放器或视频播放器和背部。

Imaging this use-case: I need to write an app that shows a list of songs and movies. When I click on one of the songs, it should open a media player and play it in a separate screen and when I click on a movie, it should open a video player and play the video. There are clear transitions here: From the main list to an audio player or a video player and back.

这样做只使用一个活动,3个片段,我就不得不更换名单片段与适当的媒体播放器片段(视频/音频),摆脱了列表的片段我自己(以preserve内存)时,用户点击一个列表项。这是所有罚款,容易做,但如果我要还添加了一个方法来添加注释歌曲/电影会发生什么?现在我要修改我的主要(唯一的)活动,了解的行为,如果有人正在播放一首歌曲,点击评论按钮,我需要再次转换到另一个片段,并允许该片段捕获/显示注释。

Doing this using only one activity and 3 fragments, I would have to replace the list fragment with an appropriate media player fragment (video/audio) and get rid of the list fragment myself (to preserve memory) when the user clicks on a list item. This is all fine and easy to do but what happens if I have to also add a way to add comments to songs/movies? Now I have to modify my main (and only) activity to understand the behavior that if someone is playing a song and clicks on the comment button, I need to transition again to yet another fragment and allow that fragment to capture/show comments.

正如你可能想象,我每次添加新的功能,我要修改我的主要活动处理的过渡,因为碎片是注定不会知道对方的,目的是相互独立的。

As you can probably imagine, every time I add a new functionality, I have to modify my main activity to handle the transition since fragments are not meant to know about each other and are meant to be independent of each other.

我最终会做,如果我只用了一个活动,是创建一个神级(我的主要活动)在我的应用程序的整个流程进行定义。保持这种流动会变得越来越复杂,因为该应用变得复杂。再次,这是怎么回事具有独立片段的原则。如果你不关心你的碎片了解对方,这是完全不同的,但那时,在我看来,你这样做是错误的:)

What I would end up doing, if I only had one activity, would be to create a God class (my main activity) where the entire flow of my application is defined. Maintaining this flow will become more and more complicated as the app becomes complicated. Again, this is going on the principle of having independent fragments. If you don't care about your fragments knowing about each other, that's entirely different but then, in my opinion, you're doing it wrong :)

我将如何做到这一点使用的活动?我仍然有3个片段,但也会有 3活动,其唯一的工作是维持过渡到/从其他活动并确定哪些片段应该被使用。清单活动之间的过渡到歌曲的活动是一个简单的意图和释放内存是为我做的。现在,如果这首歌活动需要过渡到评论的活动,它只是另一个意图从歌曲活动注释活动。我没有改变列表活性在所有与实际上,列表活动将不会知道关于此评论活动的存在。

How would I do it using activities? I would still have 3 fragments but also would have 3 activities whose only job is to maintain the transitions to/from other activities and defining which fragment should be used. Transitioning between list activity to song activity is a simple intent and the freeing up memory is done for me. Now if the song activity needs to transition to a comment activity, it's simply another intent to a comment activity from the song activity. I don't have to change the list activity at all and in fact, the list activity will never know about the existence of this comment activity.

我知道这是一个非常简单的例子,但作为您的应用程序变得越来越复杂,当你添加越来越多的使用情况,您会发现有多个活动很有意义。你不想维护自己的碎片之间的转换,否则你会最终有一个非常复杂的框架,这将慢慢变得难以维护。

I know it's a very simplified example but as your app gets more and more complex and as you add more and more use-cases, you'll find that having multiple activities makes sense. You don't want to maintain the transitions between fragments yourself, otherwise you'll end up with a very complex framework which would slowly become unmaintainable.

相同逻辑将适用于具有不同片段/布局取决于方向和屏幕的尺寸。再次,通过各种手段,你可以在你的唯一活动处理这些自己,但问题就来了:为什么?

Same logic would apply to having different fragments/layout depending on the orientation and the size of your screen. Again, by all means, you can handle these yourself in your only activity but the question becomes: why?

这篇关于为什么不经常使用的片段与一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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