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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:20:55  来源:igfitidea点击:

What is the difference between Fragment and FragmentActivity?

androidandroid-fragmentsandroid-fragmentactivity

提问by Kerry

My question is apart from the obvious inheritance differences, what are the main differences between Fragmentand 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 Fragmentis 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 Activityis running.
  • 它自己的生命周期
  • 接收自己的输入事件
  • 可以在Activity运行时添加或删除。

A Fragmentmust always be embedded in an Activity.

AFragment必须始终嵌入到Activity.

Fragmentsare not part of the API prior to HoneyComb (3.0). If you want to use Fragmentsin an app targeting a platform version prior to HoneyComb, you need to add the Support Packageto your project and use the FragmentActivityto hold your Fragments. The FragmentActivityclass has an API for dealing with Fragments, whereas the Activityclass, 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 Activityand not FragmentActivityto hold your Fragments.

如果您的项目仅针对 HoneyComb 或更新版本,则应使用Activity而不是FragmentActivityFragments.

Some details:

一些细节:

Use android.app.Fragmentwith Activity. Use android.support.v4.app.Fragmentwith FragmentActivity. Don't add the support package Fragmentto an Activityas it will cause an Exception to be thrown.

android.app.Fragment与 一起使用Activityandroid.support.v4.app.Fragment与 一起使用FragmentActivity。不要将支持包添加Fragment到 an 中,Activity因为它会导致抛出异常。

A thing to be careful with: FragmentManagerand LoaderManagerhave separate support versions for FragmentActivity:

需要注意的事情:FragmentManagerLoaderManager为 FragmentActivity 提供单独的支持版本:

If you are using a Fragmentin an Activity(HoneyComb and up), call

如果您FragmentActivity(HoneyComb 及以上)中使用 a ,请致电

  • getFragmentManager()to get android.app.FragmentManager
  • getLoaderManager()to get android.app.LoaderManager
  • getFragmentManager()要得到 android.app.FragmentManager
  • getLoaderManager()要得到 android.app.LoaderManager

if you are using a Fragmentin a FragmentActivity(pre-HoneyComb), call:

如果您FragmentFragmentActivity(HoneyComb 之前)中使用 a ,请致电:

  • getSupportFragmentManager()to get android.support.v4.app.FragmentManager.
  • getSupportLoaderManager()to get android.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 Activityit doesn't have to be part of the Activitylayout. 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.

有了这些新概念,您的代码和布局将变得更加灵活和可维护。如果你在谷歌上搜索有很多例子。