Fragment 和 FragmentActivity 有什么区别? [英] What is the difference between Fragment and FragmentActivity?

查看:47
本文介绍了Fragment 和 FragmentActivity 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题除了明显的继承差异之外,之间的主要区别是什么FragmentFragmentActivity?每个班级最适合什么场景?我试图了解为什么这两个类都存在...

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...

推荐答案

A FragmentActivity 的一部分,它有:

A Fragment is a section of an Activity, which has:

  • 它自己的生命周期
  • 接收自己的输入事件
  • 可以在 Activity 运行时添加或删除.
  • its own lifecycle
  • receives its own input events
  • can be added or removed while the Activity is running.

Fragment 必须始终嵌入到 Activity 中.

A Fragment must always be embedded in an Activity.

Fragments 不是 HoneyComb (3.0) 之前的 API 的一部分.如果您想在针对 HoneyComb 之前的平台版本的应用程序中使用 Fragments,您需要添加 支持包到您的项目并使用FragmentActivity 保存你的 Fragments.FragmentActivity 类具有用于处理 Fragments 的 API,而在 HoneyComb 之前的 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 Package to 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.

如果您的项目仅针对 HoneyComb 或更新版本,您应该使用 Activity 而不是 FragmentActivity 来保存您的 Fragments.

If your project is targeting HoneyComb or newer only, you should use Activity and not FragmentActivity to hold your Fragments.

一些细节:

使用 android.app.FragmentActivity.将 android.support.v4.app.FragmentFragmentActivity 结合使用.不要将支持包 Fragment 添加到 Activity 中,因为它会导致抛出异常.

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.

需要注意的一点:FragmentManagerLoaderManager 对 FragmentActivity 有不同的支持版本:

A thing to be careful with: FragmentManager and LoaderManager have separate support versions for FragmentActivity:

如果您在 Activity(HoneyComb 及更高版本)中使用 Fragment,请调用

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

  • getFragmentManager() 获取android.app.FragmentManager
  • getLoaderManager() 获取android.app.LoaderManager
  • getFragmentManager() to get android.app.FragmentManager
  • getLoaderManager() to get android.app.LoaderManager

如果您在 FragmentActivity(HoneyComb 之前)中使用 Fragment,请调用:

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

  • getSupportFragmentManager() 获取android.support.v4.app.FragmentManager.
  • getSupportLoaderManager() 获取android.support.v4.app.LoaderManager
  • getSupportFragmentManager() to get android.support.v4.app.FragmentManager.
  • getSupportLoaderManager() to get android.support.v4.app.LoaderManager

所以,不要

//don't do this
myFragmentActivity.getLoaderManager(); 
//instead do this:
myFragmentActivity.getSupportLoaderManager();

//don't do this:
android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
//instead do this:
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()

另外要知道的是,虽然片段必须嵌入到 Activity 中,但它不一定是 Activity 布局的一部分.它可以用作 Activity 的隐形工作器,没有自己的 UI.

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.

这篇关于Fragment 和 FragmentActivity 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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