博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fragment与FragmentActivity的关系
阅读量:4439 次
发布时间:2019-06-07

本文共 2112 字,大约阅读时间需要 7 分钟。

前阵用viewpaper+fragment做滑动引导,查阅了下网上的资料,发现在有两种做法,一个是自建类直接继承Activity另一种是继承FragmentActivity,很是迷惑,在查了些google的官方文档和StackOverflow之后有了些理解,在此坐下记录。下面的英文说明取自Stackoverflow,个人感觉解释的很清楚。

A  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 Fragment must always be embedded in an 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  to your project and use the  to hold your Fragments. The FragmentActivity class has an API for dealing with Fragments, whereas the Activity class, prior to HoneyComb, doesn't.

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

Some details:

Use android.app.Fragment with Activity. Use android.support.v4.app.Fragment withFragmentActivity. Don't add the support package Fragment to an Activity as it will cause an Exception to be thrown.

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() to get android.app.FragmentManager
  • getLoaderManager() to get android.app.LoaderManager

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

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

so, dont do

 

or

 

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.

总结来说就是标红记录的说明:

1、fragmentactivity 继承自activity,用来解决android3.0 之前没有fragment的api,所以在使用的时候需要导入support包,同时继承fragmentActivity,这样在activity中就能嵌入fragment来实现你想要的布局效果。

2、当然3.0之后你就可以直接继承自Activity,并且在其中嵌入使用fragment了。

3、获得Manager的方式也不同

3.0以下:getSupportFragmentManager()

3.0以上:getFragmentManager()

转载于:https://www.cnblogs.com/coolwxb/p/3504948.html

你可能感兴趣的文章
软件工程 第一次作业
查看>>
Content Server HA搭建
查看>>
vue-textarea 自适应高度
查看>>
(2)数据结构——线性表(链表)实现
查看>>
Java DecimalFormat的主要功能及使用方法
查看>>
JAVA基础篇NO2--Java中的基本命名规则及数据类型
查看>>
spark- PySparkSQL之PySpark解析Json集合数据
查看>>
LeetCode 219. Contains Duplicate II
查看>>
gulp 教程
查看>>
工具或安全监测网站(不定时更新)
查看>>
BigDecimal加减乘除
查看>>
多线程-多线程基础
查看>>
DotNetBar的初步使用
查看>>
mysql 恢复备份时出错 Unknown command ‘\”
查看>>
【原创】Mac上编译Hadoop1.0.3出现的一些问题
查看>>
0305
查看>>
线程 详解
查看>>
pgrep -f 和pkill -f
查看>>
51 Nod 1029 大数除法【Java大数乱搞】
查看>>
吴恩达“机器学习”——学习笔记一
查看>>