getFragmentManager().beginTransaction()就是获取相关Activity的 FragmentTransaction.
add(int containerViewId, Fragment fragment);用来添加Fragment到指定的容器内,这个容器可以是FrameLayout也可以是其他。
add(int containerViewId, Fragment fragment);用来替换Fragment到指定的容器内,这个容器可以是FrameLayout也可以是其他。
remove(Fragment fragment);)用来删除当前activity的Fragment
上面的过程到底是如何实现的呢?,我们来研究一下相关的具体的源代码。
关于Activity中的Fragmentmanager到底是什么?见Code:
Activity中定义了 FragmentManager的实例:
1
|
final
FragmentManagerImpl
mFragments
=
new
FragmentManagerImpl
(
)
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
final
class
FragmentManagerImpl
extends
FragmentManager
{
static
boolean
DEBUG
=
false
;
static
final
String
TAG
=
"FragmentManager"
;
static
final
String
TARGET_REQUEST_CODE_STATE_TAG
=
"android:target_req_state"
;
static
final
String
TARGET_STATE_TAG
=
"android:target_state"
;
static
final
String
VIEW_STATE_TAG
=
"android:view_state"
;
static
final
String
USER_VISIBLE_HINT_TAG
=
"android:user_visible_hint"
;
ArrayList
<
Runnable
>
mPendingActions
;
Runnable
[
]
mTmpActions
;
boolean
mExecutingActions
;
ArrayList
<
Fragment
>
mActive
;
ArrayList
<
Fragment
>
mAdded
;
ArrayList
<
Integer
>
mAvailIndices
;
ArrayList
<
BackStackRecord
>
mBackStack
;
ArrayList
<
Fragment
>
mCreatedMenus
;
// Must be accessed while locked.
ArrayList
<
BackStackRecord
>
mBackStackIndices
;
ArrayList
<
Integer
>
mAvailBackStackIndices
;
|
可以发现 FragmentManager其实就是一个容器用来存储Fragments。见:
1
2
|
ArrayList
<
Fragment
>
mActive
;
ArrayList
<
Fragment
>
mAdded
;
|
存储和获取Fragment的code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@
Override
public
void
putFragment
(
Bundle
bundle
,
String
key
,
Fragment
fragment
)
{
if
(
fragment
.
mIndex
<
0
)
{
throwException
(
new
IllegalStateException
(
"Fragment "
+
fragment
+
" is not currently in the FragmentManager"
)
)
;
}
bundle
.
putInt
(
key
,
fragment
.
mIndex
)
;
}
@
Override
public
Fragment
getFragment
(
Bundle
bundle
,
String
key
)
{
int
index
=
bundle
.
getInt
(
key
,
-
1
)
;
if
(
index
==
-
1
)
{
return
null
;
}
if
(
index
>=
mActive
.
size
(
)
)
{
throwException
(
new
IllegalStateException
(
"Fragement no longer exists for key "
+
key
+
": index "
+
index
)
)
;
}
Fragment
f
=
mActive
.
get
(
index
)
;
if
(
f
==
null
)
{
throwException
(
new
IllegalStateException
(
"Fragement no longer exists for key "
+
key
+
": index "
+
index
)
)
;
}
return
f
;
}
|
FragmentManger.beginFragmentTransaction()做了什么:
1
2
3
4
|
@
Override
public
FragmentTransaction
beginTransaction
(
)
{
return
new
BackStackRecord
(
this
)
;
}
|
可以发现,这里的this就是Actvity中具体的FragmentManager,关于BackStackRecord 的实现:
1
2
3
4
|
final
class
BackStackRecord
extends
FragmentTransaction
implements
FragmentManager
.
BackStackEntry
,
Runnable
{
static
final
String
TAG
=
FragmentManagerImpl
.
TAG
;
final
FragmentManagerImpl
mManager
;
|
当调用添加FragmentTransaction的add接口的时候,具体实现如下:
1
2
3
4
|
public
FragmentTransaction
add
(
int
containerViewId
,
Fragment
fragment
)
{
doAddOp
(
containerViewId
,
fragment
,
null
,
OP_ADD
)
;
return
this
;
}
|
doAddOp方法的具体实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
void
doAddOp
(
int
containerViewId
,
Fragment
fragment
,
String
tag
,
int
opcmd
)
{
fragment
.
mFragmentManager
=
mManager
;
if
(
tag
!=
null
)
{
if
(
fragment
.
mTag
!=
null
&&
!
tag
.
equals
(
fragment
.
mTag
)
)
{
throw
new
IllegalStateException
(
"Can't change tag of fragment "
+
fragment
+
": was "
+
fragment
.
mTag
+
" now "
+
tag
)
;
}
fragment
.
mTag
=
tag
;
}
if
(
containerViewId
!=
0
)
{
if
(
fragment
.
mFragmentId
!=
0
&&
fragment
.
mFragmentId
!=
containerViewId
)
{
throw
new
IllegalStateException
(
"Can't change container ID of fragment "
+
fragment
+
": was "
+
fragment
.
mFragmentId
+
" now "
+
containerViewId
)
;
}
fragment
.
mContainerId
=
fragment
.
mFragmentId
=
containerViewId
;
}
Op
op
=
new
Op
(
)
;
op
.
cmd
=
opcmd
;
op
.
fragment
=
fragment
;
addOp
(
op
)
;
}
|
addOp方法的具体实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
void
addOp
(
Op
op
)
{
if
(
mHead
==
null
)
{
mHead
=
mTail
=
op
;
}
else
{
op
.
prev
=
mTail
;
mTail
.
next
=
op
;
mTail
=
op
;
}
op
.
enterAnim
=
mEnterAnim
;
op
.
exitAnim
=
mExitAnim
;
op
.
popEnterAnim
=
mPopEnterAnim
;
op
.
popExitAnim
=
mPopExitAnim
;
mNumOp
++
;
}
|
关于Op这个class的定义和结构:
1
2
3
4
5
6
7
8
9
10
11
|
static
final
class
Op
{
Op
next
;
Op
prev
;
int
cmd
;
Fragment
fragment
;
int
enterAnim
;
int
exitAnim
;
int
popEnterAnim
;
int
popExitAnim
;
ArrayList
}
|
由上面的步骤就可以发现。FragmentManager 是始终贯穿在 Activity、FragementTransaction之间,用来存储Fragments。至于添加和删除的具体实现,大家去读读源代码吧。