Android Fragment 和 FragmentActivity 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10609268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Fragment and FragmentActivity?
提问by Kerry
My question is apart from the obvious inheritance differences, what are the main differences between Fragment
and FragmentActivity
? To what scenarios are each class best suited? I'm trying to get an understanding of why both of these classes exist...
我的问题是除了明显的继承差异之外,Fragment
和之间的主要区别是FragmentActivity
什么?每个班级最适合什么场景?我试图了解为什么这两个类都存在......
回答by Gunnar Karlsson
A Fragment
is a section of an Activity
, which has:
AFragment
是 a 的一部分Activity
,它具有:
- its own lifecycle
- receives its own input events
- can be added or removed while the
Activity
is running.
- 它自己的生命周期
- 接收自己的输入事件
- 可以在
Activity
运行时添加或删除。
A Fragment
must always be embedded in an Activity
.
AFragment
必须始终嵌入到Activity
.
Fragments
are not part of the API prior to HoneyComb (3.0). If you want to use Fragments
in an app targeting a platform version prior to HoneyComb, you need to add the Support Packageto your project and use the FragmentActivity
to hold your Fragments
. The FragmentActivity
class has an API for dealing with Fragments
, whereas the Activity
class, prior to HoneyComb, doesn't.
Fragments
在 HoneyComb (3.0) 之前不是 API 的一部分。如果您想Fragments
在针对 HoneyComb 之前的平台版本的应用程序中使用,您需要将支持包添加到您的项目中并使用FragmentActivity
来保存您的Fragments
. 本FragmentActivity
类有处理的API Fragments
,而Activity
类,前蜂窝,其实不然。
If your project is targeting HoneyComb or newer only, you should use Activity
and not FragmentActivity
to hold your Fragments
.
如果您的项目仅针对 HoneyComb 或更新版本,则应使用Activity
而不是FragmentActivity
将Fragments
.
Some details:
一些细节:
Use android.app.Fragment
with Activity
. Use android.support.v4.app.Fragment
with FragmentActivity
. Don't add the support package Fragment
to an Activity
as it will cause an Exception to be thrown.
android.app.Fragment
与 一起使用Activity
。android.support.v4.app.Fragment
与 一起使用FragmentActivity
。不要将支持包添加Fragment
到 an 中,Activity
因为它会导致抛出异常。
A thing to be careful with: FragmentManager
and LoaderManager
have separate support versions for FragmentActivity:
需要注意的事情:FragmentManager
并LoaderManager
为 FragmentActivity 提供单独的支持版本:
If you are using a Fragment
in an Activity
(HoneyComb and up), call
如果您Fragment
在Activity
(HoneyComb 及以上)中使用 a ,请致电
getFragmentManager()
to getandroid.app.FragmentManager
getLoaderManager()
to getandroid.app.LoaderManager
getFragmentManager()
要得到android.app.FragmentManager
getLoaderManager()
要得到android.app.LoaderManager
if you are using a Fragment
in a FragmentActivity
(pre-HoneyComb), call:
如果您Fragment
在FragmentActivity
(HoneyComb 之前)中使用 a ,请致电:
getSupportFragmentManager()
to getandroid.support.v4.app.FragmentManager
.getSupportLoaderManager()
to getandroid.support.v4.app.LoaderManager
getSupportFragmentManager()
得到android.support.v4.app.FragmentManager
。getSupportLoaderManager()
要得到android.support.v4.app.LoaderManager
so, don'tdo
所以,不要这样做
//don't do this
myFragmentActivity.getLoaderManager();
//instead do this:
myFragmentActivity.getSupportLoaderManager();
or
或者
//don't do this:
android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
//instead do this:
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()
Also useful to know is that while a fragment has to be embedded in an Activity
it doesn't have to be part of the Activity
layout. It can be used as an invisible worker for the activity, with no UI of its own.
同样有用的是,虽然片段必须嵌入在一个片段中,Activity
但它不一定是Activity
布局的一部分。它可以用作活动的隐形工人,没有自己的 UI。
回答by G?khan Bar?? Aker
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
FragmentActivity 是我们经典的支持片段的 Activity,仅此而已。因此需要 FragmentActivity,当 Fragment 将附加到 Activity 时。
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
好吧,Fragment 是一个很好的复制 Activity 基本行为的组件,仍然不是像 Activity 那样独立的应用程序组件,需要附加到 Activity 才能工作。
Look herefor more details
看这里了解更多详情
回答by j2emanue
Think of FragmentActivity as a regular Activity class that can support Fragments. Prior to honeycomb, an activity class could not supoprt Fragments directly, so this is needed in activities that use Fragments.
将 FragmentActivity 视为可以支持 Fragment 的常规 Activity 类。在蜂窝之前,活动类不能直接支持片段,因此在使用片段的活动中需要这样做。
If your target distribution is Honeycomb and beyond you can extend off of Activity instead.
如果您的目标发行版是 Honeycomb 及以上,您可以改为扩展 Activity。
Also a fragment is to be considered as a 'sub-activity'. It cannot exist without an activity. Always think of a fragment as a sub-activity and you should be good. So the activity would be the parent and the fragment(s) the child kind of symbolic relationship.
片段也将被视为“子活动”。没有活动就不可能存在。始终将片段视为子活动,您应该很好。因此,活动将是父级,而片段是子级的象征关系。
回答by Simone Casagranda
a FragmentActivity is an ad-hoc activity that contains Fragment. In these few words I have explain you one of the main important changes that, with android 3.0(HoneyComb), android team has inserted in the android sdk.
FragmentActivity 是一个包含 Fragment 的临时活动。在这几句话中,我向您解释了在 android 3.0(HoneyComb) 中,android 团队在 android sdk 中插入的主要重要更改之一。
With these new kind of concept your pieces of code and layout becomes more flexible and maintainable. If you search on google there are a lot of examples.
有了这些新概念,您的代码和布局将变得更加灵活和可维护。如果你在谷歌上搜索有很多例子。