片段与FragmentActivity的区别 [英] Difference between Fragment And FragmentActivity

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

问题描述

我的问题是,除了明显的遗传差异,之间有什么 片段 和<一href="http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html"><$c$c>FragmentActivity?在什么情况下是每个类最适合?我想知道为什么这两个类存在的认识...

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 <一个href="http://developer.android.com/guide/topics/fundamentals/fragments.html"><$c$c>Fragment活动,其中有一个部分:

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

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

A 片段必须嵌入活动

片段不是之前的蜂窝(3.0)的API的一部分。如果你想使用片段在一个应用程序针对一个平台版本之前的蜂窝状,你需要添加的支持包您的项目并使用<一个href="http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html"><$c$c>FragmentActivity握住你的片段。该 FragmentActivity 类有处理片段,而活动类,之前蜂窝状,其实不然。

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.

如果你的项目是针对蜂窝或更新而已,你应该使用活动,而不是 FragmentActivity 来牵你的片段

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

一些细节:

使用 android.app.Fragment 活动。使用 android.support.v4.app.Fragment FragmentActivity 。不支持包片段添加到活动,因为它会导致异常被抛出。

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.

一个事情要小心: FragmentManager LoaderManager 有单独的支持版本FragmentActivity:

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

如果您使用的是片段活动(蜂窝)之后,调用

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 (pre-蜂窝),拨打:

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

所以, DO

//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()

另外需要了解的是,当一个片段已被嵌入活动它并没有成为活动的一部分的布局。它可以作为一个不可见的工人为活动,拥有自己的没有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.

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

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