片段与FragmentActivity的区别 [英] Difference between Fragment And FragmentActivity
问题描述
我的问题是,除了明显的遗传差异,之间有什么 片段
和<一href="http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html"><$c$c>FragmentActivity$c$c>?在什么情况下是每个类最适合?我想知道为什么这两个类存在的认识...
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$c$c>是活动
,其中有一个部分:
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$c$c>握住你的片段
。该 FragmentActivity
类有处理片段
,而活动$ C的API $ C>类,之前蜂窝状,其实不然。
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 getandroid.app.FragmentManager
getLoaderManager()
to getandroid.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 getandroid.support.v4.app.FragmentManager
.getSupportLoaderManager()
to getandroid.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屋!