采用活动或片段的Andr​​oid应用程序 [英] Use Activity or Fragment in Android Application

查看:121
本文介绍了采用活动或片段的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的机器人。我经常使用的活动从一个屏幕切换到其他功能,另一个屏幕。例如,从主页到流行的网页。在那之后,我知道的片段,但我从来没有使用它。所以,如果我有多个选项卡中的应用程序在屏幕上,而不是用TabHost这里。每个标签很不同势,EX的功能:标签首页,热门标签,标签新闻,标签资料...像Instagram的应用程序。我必须使用

I am new in android. I often use Activity to change from one screen to another screen with other function. Example from Home Page to Popular page. After that, i know about fragment but i never use it before. So, if i have a application with multi tab on a screen, not use TabHost here. Function of every tab very diffrent, ex : tab Home, tab Popular, tab News, tab Profile ... like Instagram App. I must use that

  1. 活动来改变屏幕到另一个屏幕,它的意思是:我有家庭活动,受欢迎的活动,......和变化活动时,改变筛网。每个活动都有每个布局。
  2. 在一个活动
  3. 使用片段。我们拥有多片段,例如HomeFragment,热门片段...昌取代片段时,切换画面。

有什么方法比较好? 我要问的时候只使用手机屏幕。 (小尺寸屏幕,而不是平板电脑)。

What way is better ? I want to ask when use only phone screen. ( small size screen, not for tablet).

推荐答案

据认为Android设备更频谱,比明确的手机与平板电脑的桶很重要。在很多情况下,您可能想显示在屏幕上屏幕的更多信息。有时,这相当于显示两个活动的一次。

It's important to think of Android devices as more of a spectrum, than clear "phone" vs. "tablet" buckets. There are many instances where you might want to show more information on screen on medium and large screens. Sometimes, this translates to showing two "Activities" at once.

使用片段需要很少的开销,但增加了可衡量的灵活性,尤其是在开发过程的早期考虑。如果正确使用的片段,以适应更大的屏幕是非常简单的。不过,也有一些陷阱,可能使片段显得更艰巨,他们其实是:

Using Fragments requires little overhead, but adds measurable flexibility, especially when considered early in the development process. If you use Fragments properly, adapting to larger screens is extremely simple. However, there are a few "gotchas" that may make Fragments appear to be more daunting that they actually are:

  • 片段类必须声明公开(如果它是一个嵌套类,它必须是静态)。

  • Fragment classes must always be declared public (if it's a nested class, it must be static).

在父活动(或 FragmentActivity ),只添加根片段如果 savedInstanceState == NULL 。如果您在管理你的片段的状态得当,一切都为你(滚动位置的EditText值等)。

In the parent Activity (or FragmentActivity), only add the root Fragment if savedInstanceState == null. If you are managing the state of your Fragment properly, everything is handled for you (scroll position, EditText values, etc).

活动必须调用通过对 onSavedInstanceState 为了让片段来正确地恢复它的状态。

The parent Activity must call through to onSavedInstanceState in order for the Fragment to properly restore it's state.

setRetainInstance(真)应仅用于无头片段。这是当使用一个片段没有UI,并且不被添加到背栈,其通常用来做生命周期依赖的工作

setRetainInstance(true) should only be used for "headless" Fragments. This is when you use a Fragment that has no UI, and isn't added to the back stack, which is typically used to do life-cycle dependent work.

在XML中声明片段不能在 FragmentTransaction (反之亦然)可以使用。

Fragments declared in XML cannot be used in a FragmentTransaction (and vice-versa).

想到的片段作为一个模块化的观点,提供了钩子(回调),以它的活动时,一些重要的东西发生。该活动决定的基础上的可用空间,是否推出一个新的活动,或显示一个新的片段

Think of a Fragment as a modular view, that provides hooks (callbacks) to it's Activity when something important happens. The Activity decides, based on the available space, whether to launch a new Activity, or show a new Fragment.

这篇关于采用活动或片段的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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