两盘的用户界面与片段VS独立活动 [英] Two panel UI with Fragments vs Separate activities

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

问题描述

我开始一个蜂窝应用,将有一个基本的两个屏的布局,一个面板上的每个部分的主要功能的权利左边为菜单和1

I am starting a Honeycomb application that will have a basic two panel layout, one panel on the left for the menu and one on the right for the main functionality of each section.

相反的是碎片的可用样本API右侧面板上显示的内容是由一个完全不同的用户界面为每个菜单选项。

Contrary to the available samples of the Fragments API the content displayed on the right panel consists of a completely different UI for each of the menu options.

这是很有诱惑力的,根据所选择的部分只需更换合适的片段,但是这将意味着只用一个活动,在整个应用程序,这听起来并不好。此外,片段的生命周期是联系在一起的活动,所以没有碎片会被杀死,直到活动就会被杀死,导致很多活着的片段。

It is tempting to just replace the right fragment according to the selected section, but this would mean using just one activity in the entire app, and this does not sound good. Moreover, the fragment's lifecycle is tied to the activity, so no fragments will be killed until the activity gets killed, resulting in a lot of fragments "alive".

然而,具有两个面板各个菜单选项不同的活性是指用于菜单的片段将具有在每一个活动被添加并且将受到跨越所有应该有一个菜单的章节不一致布局。

However, having a different activity with two panels for every menu option means that the fragment used for the menu will have to be added in EVERY activity and will be subject to inconsistent layouts across all the sections that should have a menu.

什么是最好的做法吗?

What are the best practices here?

推荐答案

博客文章总结了原因,在活动选择片段:

This blog post summarizes the reasons for choosing fragments over activities:

嵌入式通过的ActivityGroup活动   是一个不错的想法,但一直   很难对付,因为活动   被设计成一个独立的   自包含的组件,而不是   与其他密切互动   活动。该片段API是多少   这个更好的解决方案,并应   被视为一个替代   内嵌活动。

Embedded Activities via ActivityGroup were a nice idea, but have always been difficult to deal with since Activity is designed to be an independent self-contained component instead of closely interacting with other activities. The Fragment API is a much better solution for this, and should be considered as a replacement for embedded activities.

护数据   实例可以完成   通过   Activity.onRetainNonConfigurationInstance(),   但是,这是相当klunky和   非显而易见的。片段替换   通过允许您保留的机制   只是整个片段实例   设置一个标志。

Retaining data across Activity instances could be accomplished through Activity.onRetainNonConfigurationInstance(), but this is fairly klunky and non-obvious. Fragment replaces that mechanism by allowing you to retain an entire Fragment instance just by setting a flag.

片段的专业化称为   DialogFragment可以很容易地显示   这是由于部分管理对话框   活动的生命周期。这取代   活动的管理对话框的API。

A specialization of Fragment called DialogFragment makes it easy to show a Dialog that is managed as part of the Activity lifecycle. This replaces Activity’s "managed dialog" APIs.

片段的另一种专业化   所谓ListFragment可以很容易地   显示数据的列表。这类似于   到现有ListActivity(具有   一些更多的功能),但应减少   有关如何展现常见的问题   一>列表与其他一些数据。

Another specialization of Fragment called ListFragment makes it easy to show a list of data. This is similar to the existing ListActivity (with a few more features), but should reduce the common question about how to show a> list with some other data.

所有片段的信息   当前连接到一个活动   通过在该框架为您保存   活动的保存实例状态和   恢复为您重新启动时。   这可以大大减少的量   状态保存和恢复则需要code   写自己。

The information about all fragments currently attached to an activity is saved for you by the framework in the activity’s saved instance state and restored for you when it restarts. This can greatly reduce the amount of state save and restore code you need to write yourself.

该框架已经内置支持   管理的背叠片段的   对象,因此很容易提供   内活动后退按钮行为   整合现有活动   回堆栈。这种状态也被保留   并自动恢复为你。

The framework has built-in support for managing a back-stack of Fragment objects, making it easy to provide intra-activity Back button behavior that integrates the existing activity back stack. This state is also saved and restored for you automatically.

片段是相当新的,所以超出那篇文章,我不知道你会发现很多最佳实践。我认为你需要做的决定是在我的相互作用紧密耦合,这意味着它们共享数据或者是他们的立场而没有太多的互动单独的组件。

Fragments are fairly new, so beyond that article, I'm not sure your going to find much for best practices. I think the decision you need to make is are my interactions tightly coupled and meant to share data or are they stand alone components which don't have much interaction.

修改,澄清的:我觉得用一个应用程序的单一活动不一定是一个错误的决定。这是真的,你应该根据你的应用程序的功能做出决定。基于这篇文章,一个活动是独立的,而一个片段,通常情况下,只有当有关在一个活动的范围,其它片段结合。你所描述的情况,不同活动的组合是他们设计的片段来解决痛点之一。

edit, clarification: I think that using a single activity for an app isn't necessarily a bad decision. It's really a decision you should make based on the functionality of your app. Based on the article, an Activity is stand alone while a fragment is, typically, only relevant when combined with other fragments in the scope of an Activity. The situation you describe, with combinations of different activities is one of the pain points they designed Fragments to solve.

这篇关于两盘的用户界面与片段VS独立活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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