Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sxychenjing
engine
提交
dbd622cf
E
engine
项目概览
sxychenjing
/
engine
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
engine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
dbd622cf
编写于
4月 26, 2021
作者:
X
xster
提交者:
GitHub
4月 26, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix crash when FlutterFragmentActivity is recreated with an existing FlutterFragment (#25770)
上级
79e0d26e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
5 deletion
+51
-5
shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java
...io/flutter/embedding/android/FlutterFragmentActivity.java
+19
-5
shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java
...lutter/embedding/android/FlutterFragmentActivityTest.java
+32
-0
未找到文件。
shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java
浏览文件 @
dbd622cf
...
...
@@ -241,6 +241,10 @@ public class FlutterFragmentActivity extends FragmentActivity
@Override
protected
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
switchLaunchThemeForNormalTheme
();
// Get an existing fragment reference first before onCreate since onCreate would re-attach
// existing fragments. This would cause FlutterFragment to reference the host activity which
// should be aware of its child fragment.
flutterFragment
=
retrieveExistingFlutterFragmentIfPossible
();
super
.
onCreate
(
savedInstanceState
);
...
...
@@ -366,21 +370,31 @@ public class FlutterFragmentActivity extends FragmentActivity
return
container
;
}
/**
* Retrieves the previously created {@link FlutterFragment} if possible.
*
* <p>If the activity is recreated, an existing {@link FlutterFragment} may already exist. Retain
* a reference to that {@link FlutterFragment} in the {@code #flutterFragment} field and avoid
* re-creating another {@link FlutterFragment}.
*/
@VisibleForTesting
FlutterFragment
retrieveExistingFlutterFragmentIfPossible
()
{
FragmentManager
fragmentManager
=
getSupportFragmentManager
();
return
(
FlutterFragment
)
fragmentManager
.
findFragmentByTag
(
TAG_FLUTTER_FRAGMENT
);
}
/**
* Ensure that a {@link FlutterFragment} is attached to this {@code FlutterFragmentActivity}.
*
* <p>If no {@link FlutterFragment} exists in this {@code FlutterFragmentActivity}, then a {@link
* FlutterFragment} is created and added. If a {@link FlutterFragment} does exist in this {@code
* FlutterFragmentActivity}, then a reference to that {@link FlutterFragment} is retained in
* {@code #flutterFragment}.
* FlutterFragment} is created and added.
*/
private
void
ensureFlutterFragmentCreated
()
{
FragmentManager
fragmentManager
=
getSupportFragmentManager
();
flutterFragment
=
(
FlutterFragment
)
fragmentManager
.
findFragmentByTag
(
TAG_FLUTTER_FRAGMENT
);
if
(
flutterFragment
==
null
)
{
// No FlutterFragment exists yet. This must be the initial Activity creation. We will create
// and add a new FlutterFragment to this Activity.
flutterFragment
=
createFlutterFragment
();
FragmentManager
fragmentManager
=
getSupportFragmentManager
();
fragmentManager
.
beginTransaction
()
.
add
(
FRAGMENT_CONTAINER_ID
,
flutterFragment
,
TAG_FLUTTER_FRAGMENT
)
...
...
shell/platform/android/test/io/flutter/embedding/android/FlutterFragmentActivityTest.java
浏览文件 @
dbd622cf
...
...
@@ -3,6 +3,7 @@ package io.flutter.embedding.android;
import
static
io
.
flutter
.
embedding
.
android
.
FlutterActivityLaunchConfigs
.
HANDLE_DEEPLINKING_META_DATA_KEY
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
spy
;
...
...
@@ -170,7 +171,37 @@ public class FlutterFragmentActivityTest {
assertTrue
(
foundCustomView
);
}
@Test
public
void
itCreatesAValidFlutterFragment
()
{
FlutterFragmentActivityWithProvidedEngine
activity
=
Robolectric
.
buildActivity
(
FlutterFragmentActivityWithProvidedEngine
.
class
).
get
();
// Creating the FlutterFragmentActivity will create and attach the FlutterFragment, causing
// a FlutterEngine to be created.
activity
.
onCreate
(
null
);
assertNotNull
(
activity
.
getFlutterEngine
());
assertEquals
(
1
,
activity
.
numberOfEnginesCreated
);
}
@Test
public
void
itRetrievesExistingFlutterFragmentWhenRecreated
()
{
FlutterFragmentActivityWithProvidedEngine
activity
=
spy
(
Robolectric
.
buildActivity
(
FlutterFragmentActivityWithProvidedEngine
.
class
).
get
());
FlutterFragment
fragment
=
mock
(
FlutterFragment
.
class
);
when
(
activity
.
retrieveExistingFlutterFragmentIfPossible
()).
thenReturn
(
fragment
);
FlutterEngine
engine
=
mock
(
FlutterEngine
.
class
);
when
(
fragment
.
getFlutterEngine
()).
thenReturn
(
engine
);
activity
.
onCreate
(
null
);
assertEquals
(
engine
,
activity
.
getFlutterEngine
());
assertEquals
(
0
,
activity
.
numberOfEnginesCreated
);
}
static
class
FlutterFragmentActivityWithProvidedEngine
extends
FlutterFragmentActivity
{
int
numberOfEnginesCreated
=
0
;
@Override
protected
FlutterFragment
createFlutterFragment
()
{
return
FlutterFragment
.
createDefault
();
...
...
@@ -184,6 +215,7 @@ public class FlutterFragmentActivityTest {
when
(
flutterJNI
.
isAttached
()).
thenReturn
(
true
);
when
(
flutterLoader
.
automaticallyRegisterPlugins
()).
thenReturn
(
true
);
numberOfEnginesCreated
++;
return
new
FlutterEngine
(
context
,
flutterLoader
,
flutterJNI
,
new
String
[]
{},
true
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录