Skip to content

Commit a045ef2

Browse files
committed
修复 WindowLifecycle.unregister 在极端情况下会出现空指针的问题
1 parent 3b59790 commit a045ef2

File tree

7 files changed

+54
-31
lines changed

7 files changed

+54
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ android {
3737
3838
dependencies {
3939
// 吐司框架:https://github.com/getActivity/ToastUtils
40-
implementation 'com.github.getActivity:ToastUtils:9.0'
40+
implementation 'com.github.getActivity:ToastUtils:9.1'
4141
}
4242
```
4343

ToastUtils.apk

-382 KB
Binary file not shown.

app/build.gradle

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.toast.demo"
88
minSdkVersion 16
99
targetSdkVersion 30
10-
versionCode 90
11-
versionName "9.0"
10+
versionCode 91
11+
versionName "9.1"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -29,12 +29,16 @@ dependencies {
2929
implementation fileTree(include: ['*.jar'], dir: 'libs')
3030
implementation project(':library')
3131
implementation 'androidx.appcompat:appcompat:1.2.0'
32-
// 标题栏:https://github.com/getActivity/TitleBar
33-
implementation 'com.hjq:titlebar:8.5'
34-
// 悬浮窗:https://github.com/getActivity/XToast
35-
implementation 'com.hjq:xtoast:6.9'
32+
33+
// 权限请求框架:https://github.com/getActivity/XXPermissions
34+
implementation 'com.github.getActivity:XXPermissions:10.8'
35+
36+
// 标题栏框架:https://github.com/getActivity/TitleBar
37+
implementation 'com.github.getActivity:TitleBar:8.5'
38+
39+
// 悬浮窗框架:https://github.com/getActivity/XToast
40+
implementation 'com.github.getActivity:XToast:6.9'
41+
3642
// 内存泄漏捕捉:https://github.com/square/leakcanary
3743
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
38-
// 权限请求框架:https://github.com/getActivity/XXPermissions
39-
implementation 'com.hjq:xxpermissions:10.6'
4044
}

build.gradle

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@
22

33
buildscript {
44
repositories {
5-
jcenter()
5+
// 阿里云云效仓库:https://maven.aliyun.com/mvn/guide
6+
maven { url 'https://maven.aliyun.com/repository/jcenter' }
7+
maven { url 'https://maven.aliyun.com/repository/google' }
8+
// 华为开源镜像:https://mirrors.huaweicloud.com/
9+
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
10+
// JitPack 远程仓库:https://jitpack.io
11+
maven { url 'https://jitpack.io' }
12+
mavenCentral()
613
google()
14+
jcenter()
715
}
816
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.4.0'
17+
classpath 'com.android.tools.build:gradle:4.1.2'
1018
}
1119
}
1220

1321
allprojects {
1422
repositories {
15-
jcenter()
23+
maven { url 'https://maven.aliyun.com/repository/jcenter' }
24+
maven { url 'https://maven.aliyun.com/repository/google' }
25+
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
26+
maven { url 'https://jitpack.io' }
27+
mavenCentral()
1628
google()
17-
maven {url "https://jitpack.io"}
18-
}
19-
20-
//解决app:mockableAndroidJar错误的问题
21-
gradle.taskGraph.whenReady {
22-
tasks.each { task ->
23-
if (task.name.equals('mockableAndroidJar')) {
24-
task.enabled = false
25-
}
26-
}
29+
jcenter()
2730
}
2831
}
2932

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
distributionBase=GRADLE_USER_HOME
2-
distributionPath=wrapper/dists
3-
zipStoreBase=GRADLE_USER_HOME
4-
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
1+
zipStoreBase = GRADLE_USER_HOME
2+
zipStorePath = wrapper/dists
3+
distributionBase = GRADLE_USER_HOME
4+
distributionPath = wrapper/dists
5+
distributionUrl = https\://services.gradle.org/distributions/gradle-6.5-all.zip

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 14
8-
versionCode 90
9-
versionName "9.0"
8+
versionCode 91
9+
versionName "9.1"
1010
}
1111

1212
// 使用 JDK 1.8

library/src/main/java/com/hjq/toast/WindowLifecycle.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public void onActivityPaused(Activity activity) {
5252
return;
5353
}
5454

55-
// 取消这个吐司的显示
56-
if (mToastImpl == null || !mToastImpl.isShow()) {
55+
if (mToastImpl == null) {
5756
return;
5857
}
5958

@@ -73,21 +72,38 @@ public void onActivityDestroyed(Activity activity) {
7372
if (mActivity != activity) {
7473
return;
7574
}
75+
76+
if (mToastImpl != null) {
77+
mToastImpl.cancel();
78+
}
79+
7680
unregister();
7781
mActivity = null;
7882
}
7983

84+
/**
85+
* 注册
86+
*/
8087
void register(ToastImpl impl) {
8188
mToastImpl = impl;
89+
if (mActivity == null) {
90+
return;
91+
}
8292
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
8393
mActivity.registerActivityLifecycleCallbacks(this);
8494
} else {
8595
mActivity.getApplication().registerActivityLifecycleCallbacks(this);
8696
}
8797
}
8898

99+
/**
100+
* 反注册
101+
*/
89102
void unregister() {
90103
mToastImpl = null;
104+
if (mActivity == null) {
105+
return;
106+
}
91107
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
92108
mActivity.unregisterActivityLifecycleCallbacks(this);
93109
} else {

0 commit comments

Comments
 (0)