Skip to content

Commit 615a890

Browse files
committed
修复使用夜间模式和屏幕旋转之后 Application 没有跟随变化的 Bug
1 parent 2b7e80f commit 615a890

14 files changed

+118
-70
lines changed

MultiLanguages.apk

418 KB
Binary file not shown.

MultiLanguages.jpg

3.96 KB
Loading

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@
88

99
#### 集成步骤
1010

11+
```groovy
12+
buildscript {
13+
......
14+
}
15+
allprojects {
16+
repositories {
17+
// JitPack 远程仓库:https://jitpack.io
18+
maven { url 'https://jitpack.io' }
19+
}
20+
}
21+
```
22+
23+
* 在项目 app 模块下的 `build.gradle` 文件中加入
24+
1125
```groovy
1226
dependencies {
1327
// 语种切换框架:https://github.com/getActivity/MultiLanguages
14-
implementation 'com.hjq:language:6.5'
28+
implementation 'com.github.getActivity:MultiLanguages:6.6'
1529
}
1630
```
1731

@@ -25,7 +39,7 @@ public final class XxxApplication extends Application {
2539
@Override
2640
public void onCreate() {
2741
super.onCreate();
28-
// 初始化语种切换框架(自动适配第三方库中 Activity 语种)
42+
// 初始化语种切换框架
2943
MultiLanguages.init(this);
3044
}
3145
}

app/build.gradle

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ android {
77
applicationId "com.hjq.language.demo"
88
minSdkVersion 16
99
targetSdkVersion 30
10-
versionCode 65
11-
versionName "6.5"
10+
versionCode 66
11+
versionName "6.6"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
14+
15+
// 支持 JDK 1.8
16+
compileOptions {
17+
targetCompatibility JavaVersion.VERSION_1_8
18+
sourceCompatibility JavaVersion.VERSION_1_8
19+
}
20+
1421
buildTypes {
1522
release {
1623
minifyEnabled false
@@ -27,9 +34,8 @@ dependencies {
2734
implementation 'com.google.android.material:material:1.2.1'
2835

2936
// 标题栏框架:https://github.com/getActivity/TitleBar
30-
implementation 'com.hjq:titlebar:8.2'
31-
// 吐司框架:https://github.com/getActivity/ToastUtils
32-
implementation 'com.hjq:toast:8.8'
33-
// 内存泄漏捕捉:https://github.com/square/leakcanary
34-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5'
37+
implementation 'com.github.getActivity:TitleBar:8.5'
38+
39+
// 内存泄漏检测:https://github.com/square/leakcanary
40+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
3541
}

app/src/main/java/com/hjq/language/demo/AppApplication.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import com.hjq.language.MultiLanguages;
88
import com.hjq.language.OnLanguageListener;
9-
import com.hjq.toast.ToastUtils;
109

1110
import java.util.Locale;
1211

@@ -21,9 +20,8 @@ public final class AppApplication extends Application {
2120
@Override
2221
public void onCreate() {
2322
super.onCreate();
24-
ToastUtils.init(this);
2523

26-
// 初始化多语种框架(自动适配第三方库中 Activity 语种)
24+
// 初始化多语种框架
2725
MultiLanguages.init(this);
2826
// 设置语种变化监听器
2927
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {

app/src/main/java/com/hjq/language/demo/MainActivity.java

+27-14
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import android.content.Intent;
44
import android.os.Bundle;
5-
import android.view.View;
6-
import android.view.ViewGroup;
75
import android.webkit.WebChromeClient;
86
import android.webkit.WebView;
97
import android.webkit.WebViewClient;
8+
import android.widget.RadioGroup;
109
import android.widget.TextView;
1110

1211
import com.hjq.language.MultiLanguages;
@@ -20,16 +19,19 @@
2019
* desc : 多语种切换演示
2120
*/
2221
public final class MainActivity extends BaseActivity
23-
implements View.OnClickListener {
22+
implements RadioGroup.OnCheckedChangeListener {
2423

2524
private WebView mWebView;
25+
private RadioGroup mRadioGroup;
2626

2727
@Override
2828
protected void onCreate(Bundle savedInstanceState) {
2929
super.onCreate(savedInstanceState);
3030
setContentView(R.layout.activity_language);
3131

3232
mWebView = findViewById(R.id.wv_language_web);
33+
mRadioGroup = findViewById(R.id.rg_language_languages);
34+
3335
mWebView.setWebViewClient(new WebViewClient());
3436
mWebView.setWebChromeClient(new WebChromeClient());
3537
mWebView.loadUrl("https://developer.android.google.cn/index.html");
@@ -38,31 +40,42 @@ protected void onCreate(Bundle savedInstanceState) {
3840
((TextView) findViewById(R.id.tv_language_application)).setText(getApplication().getResources().getString(R.string.current_language));
3941
((TextView) findViewById(R.id.tv_language_system)).setText(MultiLanguages.getLanguageString(this, MultiLanguages.getSystemLanguage(), R.string.current_language));
4042

41-
findViewById(R.id.btn_language_auto).setOnClickListener(this);
42-
findViewById(R.id.btn_language_cn).setOnClickListener(this);
43-
findViewById(R.id.btn_language_tw).setOnClickListener(this);
44-
findViewById(R.id.btn_language_en).setOnClickListener(this);
43+
if (MultiLanguages.isSystemLanguage()) {
44+
mRadioGroup.check(R.id.rb_language_auto);
45+
} else {
46+
Locale locale = MultiLanguages.getAppLanguage();
47+
if (Locale.CHINA.equals(locale)) {
48+
mRadioGroup.check(R.id.rb_language_cn);
49+
} else if (Locale.TAIWAN.equals(locale)) {
50+
mRadioGroup.check(R.id.rb_language_tw);
51+
} else if (Locale.ENGLISH.equals(locale)) {
52+
mRadioGroup.check(R.id.rb_language_en);
53+
} else {
54+
mRadioGroup.check(R.id.rb_language_auto);
55+
}
56+
}
57+
58+
mRadioGroup.setOnCheckedChangeListener(this);
4559
}
4660

4761
/**
48-
* {@link View.OnClickListener}
62+
* {@link RadioGroup.OnCheckedChangeListener}
4963
*/
5064
@Override
51-
public void onClick(View v) {
65+
public void onCheckedChanged(RadioGroup group, int checkedId) {
5266
// 是否需要重启
5367
boolean restart = false;
5468

55-
int viewId = v.getId();
56-
if (viewId == R.id.btn_language_auto) {
69+
if (checkedId == R.id.rb_language_auto) {
5770
// 跟随系统
5871
restart = MultiLanguages.setSystemLanguage(this);
59-
} else if (viewId == R.id.btn_language_cn) {
72+
} else if (checkedId == R.id.rb_language_cn) {
6073
// 简体中文
6174
restart = MultiLanguages.setAppLanguage(this, Locale.CHINA);
62-
} else if (viewId == R.id.btn_language_tw) {
75+
} else if (checkedId == R.id.rb_language_tw) {
6376
// 繁体中文
6477
restart = MultiLanguages.setAppLanguage(this, Locale.TAIWAN);
65-
} else if (viewId == R.id.btn_language_en) {
78+
} else if (checkedId == R.id.rb_language_en) {
6679
// 英语
6780
restart = MultiLanguages.setAppLanguage(this, Locale.ENGLISH);
6881
}

app/src/main/res/layout/activity_language.xml

+19-18
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,57 @@
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
1515
app:backButton="false"
16-
app:title="https://github.com/getActivity/MultiLanguages"
17-
app:titleSize="13sp" />
16+
app:title="@string/app_name" />
1817

1918
<com.hjq.language.demo.LanguagesWebView
2019
android:id="@+id/wv_language_web"
2120
android:layout_width="match_parent"
2221
android:layout_height="0px"
23-
android:layout_marginTop="20dp"
22+
android:layout_marginTop="10dp"
2423
android:layout_weight="1" />
2524

2625
<View
2726
android:layout_width="match_parent"
2827
android:layout_height="1px"
2928
android:background="#dcdcdc" />
3029

31-
<LinearLayout
32-
android:layout_marginTop="20dp"
33-
android:layout_marginBottom="20dp"
30+
<RadioGroup
3431
android:id="@+id/rg_language_languages"
3532
android:layout_width="match_parent"
3633
android:layout_height="wrap_content"
34+
android:layout_marginTop="20dp"
3735
android:gravity="center_horizontal"
3836
android:orientation="horizontal">
3937

40-
<Button
41-
android:id="@+id/btn_language_auto"
38+
<RadioButton
39+
android:id="@+id/rb_language_auto"
4240
android:layout_width="wrap_content"
4341
android:layout_height="wrap_content"
4442
android:checked="true"
4543
android:text="自动" />
4644

47-
<Button
48-
android:id="@+id/btn_language_cn"
45+
<RadioButton
46+
android:id="@+id/rb_language_cn"
4947
android:layout_width="wrap_content"
5048
android:layout_height="wrap_content"
49+
android:layout_marginLeft="20dp"
5150
android:text="简体" />
5251

53-
<Button
54-
android:id="@+id/btn_language_tw"
52+
<RadioButton
53+
android:id="@+id/rb_language_tw"
5554
android:layout_width="wrap_content"
5655
android:layout_height="wrap_content"
56+
android:layout_marginLeft="20dp"
5757
android:text="繁体" />
5858

59-
<Button
60-
android:id="@+id/btn_language_en"
59+
<RadioButton
60+
android:id="@+id/rb_language_en"
6161
android:layout_width="wrap_content"
6262
android:layout_height="wrap_content"
63+
android:layout_marginLeft="20dp"
6364
android:text="英语" />
6465

65-
</LinearLayout>
66+
</RadioGroup>
6667

6768
<LinearLayout
6869
android:layout_width="wrap_content"
@@ -86,7 +87,7 @@
8687
<LinearLayout
8788
android:layout_width="wrap_content"
8889
android:layout_height="wrap_content"
89-
android:layout_marginTop="40dp"
90+
android:layout_marginTop="20dp"
9091
android:orientation="horizontal">
9192

9293
<TextView
@@ -104,8 +105,8 @@
104105
<LinearLayout
105106
android:layout_width="wrap_content"
106107
android:layout_height="wrap_content"
107-
android:layout_marginTop="40dp"
108-
android:layout_marginBottom="40dp"
108+
android:layout_marginTop="20dp"
109+
android:layout_marginBottom="20dp"
109110
android:orientation="horizontal">
110111

111112
<TextView

build.gradle

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ buildscript {
55
}
66
dependencies {
77
classpath 'com.android.tools.build:gradle:3.4.0'
8-
classpath 'com.novoda:bintray-release:0.9.2'
98
}
109
}
1110

1211
allprojects {
1312
repositories {
13+
maven {url "https://jitpack.io"}
1414
jcenter()
1515
google()
16-
maven {url "https://jitpack.io"}
17-
maven {url 'https://maven.google.com'}
1816
}
1917
}
2018

library/build.gradle

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
apply plugin: 'com.android.library'
2-
apply plugin: 'com.novoda.bintray-release'
32

43
android {
54
compileSdkVersion 26
65

76
defaultConfig {
87
minSdkVersion 14
9-
versionCode 65
10-
versionName "6.5"
8+
versionCode 66
9+
versionName "6.6"
1110
}
1211
}
1312

14-
publish {
15-
userOrg = 'getactivity'
16-
groupId = 'com.hjq'
17-
artifactId = 'language'
18-
publishVersion = '6.5'
19-
desc = 'Android Multi Languages Adaptation Framework'
20-
website = "https://github.com/getActivity/MultiLanguages"
21-
}
22-
2313
tasks.withType(Javadoc) {
2414
options.addStringOption('Xdoclint:none', '-quiet')
2515
options.addStringOption('encoding', 'UTF-8')

library/src/main/java/com/hjq/language/ActivityLanguages.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* author : Android 轮子哥
99
* github : https://github.com/getActivity/MultiLanguages
1010
* time : 2021/01/21
11-
* desc : Activity 语种绑定
11+
* desc : Activity 语种注入
1212
*/
1313
final class ActivityLanguages implements Application.ActivityLifecycleCallbacks {
1414

library/src/main/java/com/hjq/language/LanguagesConfig.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.SharedPreferences;
5+
import android.text.TextUtils;
56

67
import java.util.Locale;
78

@@ -17,7 +18,8 @@ final class LanguagesConfig {
1718
private static final String KEY_COUNTRY = "key_country";
1819

1920
private static String sSharedPreferencesName = "language_setting";
20-
private static Locale sCurrentLanguage;
21+
22+
private static volatile Locale sCurrentLanguage;
2123

2224
static void setSharedPreferencesName(String name) {
2325
sSharedPreferencesName = name;
@@ -39,7 +41,7 @@ static Locale getAppLanguage(Context context) {
3941
if (sCurrentLanguage == null) {
4042
String language = getSharedPreferences(context).getString(KEY_LANGUAGE, null);
4143
String country = getSharedPreferences(context).getString(KEY_COUNTRY, null);
42-
if (language != null && !"".equals(language)) {
44+
if (!TextUtils.isEmpty(language)) {
4345
sCurrentLanguage = new Locale(language, country);
4446
} else {
4547
sCurrentLanguage = LanguagesUtils.getLocale(context);

library/src/main/java/com/hjq/language/LanguagesObserver.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.hjq.language;
22

3+
import android.app.Application;
34
import android.content.ComponentCallbacks;
4-
import android.content.Context;
55
import android.content.res.Configuration;
66
import android.content.res.Resources;
77

@@ -33,18 +33,22 @@ static Locale getSystemLanguage() {
3333
/**
3434
* 注册系统语种变化监听
3535
*/
36-
static void register(Context context) {
37-
context.registerComponentCallbacks(new LanguagesObserver());
36+
static void register(Application application) {
37+
application.registerComponentCallbacks(new LanguagesObserver());
3838
}
3939

4040
/**
41-
* 手机的语种发生了变化
41+
* 手机的配置发生了变化
4242
*/
4343
@Override
4444
public void onConfigurationChanged(Configuration newConfig) {
4545
Locale newLocale = LanguagesUtils.getLocale(newConfig);
4646

4747
Locale oldLocale = sSystemLanguage;
48+
49+
// 更新 Application 的配置,否则会出现横竖屏切换之后 Application 的 orientation 没有随之变化的问题
50+
LanguagesUtils.updateConfigurationChanged(MultiLanguages.getApplication(), newConfig);
51+
4852
if (newLocale.equals(oldLocale)) {
4953
return;
5054
}

0 commit comments

Comments
 (0)