Skip to content

Commit

Permalink
修复 App 退回到后台后切换语言导致 Application 语种没有及时刷新的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Jul 6, 2021
1 parent 615a890 commit be92a8f
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 45 deletions.
Binary file modified MultiLanguages.apk
Binary file not shown.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

```groovy
buildscript {
......
repositories {
maven { url 'https://jitpack.io' }
}
}
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
Expand All @@ -25,7 +26,7 @@ allprojects {
```groovy
dependencies {
// 语种切换框架:https://github.com/getActivity/MultiLanguages
implementation 'com.github.getActivity:MultiLanguages:6.6'
implementation 'com.github.getActivity:MultiLanguages:6.8'
}
```

Expand Down
15 changes: 10 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.language.demo"
minSdkVersion 16
targetSdkVersion 30
versionCode 66
versionName "6.6"
versionCode 68
versionName "6.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand All @@ -30,11 +30,16 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
// AppCompat 库:https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
implementation 'androidx.appcompat:appcompat:1.3.0'
// Material 库:https://github.com/material-components/material-components-android
implementation 'com.google.android.material:material:1.3.0'

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:8.5'
implementation 'com.github.getActivity:TitleBar:8.6'

// 吐司框架:https://github.com/getActivity/ToastUtils
implementation 'com.github.getActivity:ToastUtils:9.5'

// 内存泄漏检测:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
android:name=".AppApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme"
tools:targetApi="q">

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/hjq/language/demo/AppApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.hjq.language.MultiLanguages;
import com.hjq.language.OnLanguageListener;
import com.hjq.toast.ToastUtils;

import java.util.Locale;

Expand All @@ -21,6 +22,9 @@ public final class AppApplication extends Application {
public void onCreate() {
super.onCreate();

// 初始化 Toast 框架
ToastUtils.init(this);

// 初始化多语种框架
MultiLanguages.init(this);
// 设置语种变化监听器
Expand Down
49 changes: 34 additions & 15 deletions app/src/main/java/com/hjq/language/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.language.MultiLanguages;
import com.hjq.toast.ToastUtils;

import java.util.Locale;

Expand All @@ -19,39 +23,43 @@
* desc : 多语种切换演示
*/
public final class MainActivity extends BaseActivity
implements RadioGroup.OnCheckedChangeListener {
implements RadioGroup.OnCheckedChangeListener, OnTitleBarListener {

private TitleBar mTitleBar;
private WebView mWebView;
private RadioGroup mRadioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);
setContentView(R.layout.activity_main);

mWebView = findViewById(R.id.wv_language_web);
mRadioGroup = findViewById(R.id.rg_language_languages);
mTitleBar = findViewById(R.id.tb_main_bar);
mWebView = findViewById(R.id.wv_main_web);
mRadioGroup = findViewById(R.id.rg_main_languages);

mTitleBar.setOnTitleBarListener(this);

mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("https://developer.android.google.cn/index.html");

//((TextView) findViewById(R.id.tv_language_activity)).setText(this.getResources().getString(R.string.current_language));
((TextView) findViewById(R.id.tv_language_application)).setText(getApplication().getResources().getString(R.string.current_language));
((TextView) findViewById(R.id.tv_language_system)).setText(MultiLanguages.getLanguageString(this, MultiLanguages.getSystemLanguage(), R.string.current_language));
((TextView) findViewById(R.id.tv_main_language_application)).setText(getApplication().getResources().getString(R.string.current_language));
((TextView) findViewById(R.id.tv_main_language_system)).setText(MultiLanguages.getLanguageString(this, MultiLanguages.getSystemLanguage(), R.string.current_language));

if (MultiLanguages.isSystemLanguage()) {
mRadioGroup.check(R.id.rb_language_auto);
mRadioGroup.check(R.id.rb_main_language_auto);
} else {
Locale locale = MultiLanguages.getAppLanguage();
if (Locale.CHINA.equals(locale)) {
mRadioGroup.check(R.id.rb_language_cn);
mRadioGroup.check(R.id.rb_main_language_cn);
} else if (Locale.TAIWAN.equals(locale)) {
mRadioGroup.check(R.id.rb_language_tw);
mRadioGroup.check(R.id.rb_main_language_tw);
} else if (Locale.ENGLISH.equals(locale)) {
mRadioGroup.check(R.id.rb_language_en);
mRadioGroup.check(R.id.rb_main_language_en);
} else {
mRadioGroup.check(R.id.rb_language_auto);
mRadioGroup.check(R.id.rb_main_language_auto);
}
}

Expand All @@ -66,16 +74,16 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
// 是否需要重启
boolean restart = false;

if (checkedId == R.id.rb_language_auto) {
if (checkedId == R.id.rb_main_language_auto) {
// 跟随系统
restart = MultiLanguages.setSystemLanguage(this);
} else if (checkedId == R.id.rb_language_cn) {
} else if (checkedId == R.id.rb_main_language_cn) {
// 简体中文
restart = MultiLanguages.setAppLanguage(this, Locale.CHINA);
} else if (checkedId == R.id.rb_language_tw) {
} else if (checkedId == R.id.rb_main_language_tw) {
// 繁体中文
restart = MultiLanguages.setAppLanguage(this, Locale.TAIWAN);
} else if (checkedId == R.id.rb_language_en) {
} else if (checkedId == R.id.rb_main_language_en) {
// 英语
restart = MultiLanguages.setAppLanguage(this, Locale.ENGLISH);
}
Expand Down Expand Up @@ -125,4 +133,15 @@ protected void onDestroy() {
//销毁此的WebView的内部状态
mWebView.destroy();
}

@Override
public void onLeftClick(View view) {}

@Override
public void onTitleClick(View view) {
ToastUtils.show(R.string.app_name);
}

@Override
public void onRightClick(View view) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
tools:context=".MainActivity">

<com.hjq.bar.TitleBar
android:id="@+id/tb_main_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backButton="false"
app:title="@string/app_name" />

<com.hjq.language.demo.LanguagesWebView
android:id="@+id/wv_language_web"
android:id="@+id/wv_main_web"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_marginTop="10dp"
Expand All @@ -28,36 +29,36 @@
android:background="#dcdcdc" />

<RadioGroup
android:id="@+id/rg_language_languages"
android:id="@+id/rg_main_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_language_auto"
android:id="@+id/rb_main_language_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="自动" />

<RadioButton
android:id="@+id/rb_language_cn"
android:id="@+id/rb_main_language_cn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="简体" />

<RadioButton
android:id="@+id/rb_language_tw"
android:id="@+id/rb_main_language_tw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="繁体" />

<RadioButton
android:id="@+id/rb_language_en"
android:id="@+id/rb_main_language_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
Expand All @@ -77,7 +78,7 @@
android:text="当前 Activity 语种:" />

<TextView
android:id="@+id/tv_language_activity"
android:id="@+id/tv_main_language_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
Expand All @@ -96,7 +97,7 @@
android:text="当前 Application 语种:" />

<TextView
android:id="@+id/tv_language_application"
android:id="@+id/tv_main_language_application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp" />
Expand All @@ -115,7 +116,7 @@
android:text="当前 System 语种:" />

<TextView
android:id="@+id/tv_language_system"
android:id="@+id/tv_main_language_system"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp" />
Expand Down
22 changes: 18 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
buildscript {
repositories {
jcenter()
// 阿里云云效仓库:https://maven.aliyun.com/mvn/guide
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
// 华为开源镜像:https://mirrors.huaweicloud.com/
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
mavenCentral()
google()
// noinspection JcenterRepositoryObsolete
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:4.1.2'
}
}

allprojects {
repositories {
maven {url "https://jitpack.io"}
jcenter()
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://repo.huaweicloud.com/repository/maven/' }
maven { url 'https://jitpack.io' }
mavenCentral()
google()
// noinspection JcenterRepositoryObsolete
jcenter()
}
}

Expand Down
5 changes: 5 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# 表示使用 AndroidX
android.useAndroidX = true
# 表示将第三方库迁移到 AndroidX
android.enableJetifier = true
11 changes: 5 additions & 6 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Wed Jul 18 11:21:06 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
zipStoreBase = GRADLE_USER_HOME
zipStorePath = wrapper/dists
distributionBase = GRADLE_USER_HOME
distributionPath = wrapper/dists
distributionUrl = https\://services.gradle.org/distributions/gradle-6.5-all.zip
11 changes: 9 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 66
versionName "6.6"
versionCode 68
versionName "6.8"
}

android.libraryVariants.all { variant ->
// aar 输出文件名配置
variant.outputs.all { output ->
outputFileName = "language-${android.defaultConfig.versionName}.aar"
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions library/src/main/java/com/hjq/language/LanguagesObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void onConfigurationChanged(Configuration newConfig) {
return;
}
sSystemLanguage = newLocale;

// 如果当前的语种是跟随系统变化的,那么就需要重置一下当前 App 的语种
if (LanguagesConfig.isSystemLanguage(MultiLanguages.getApplication())) {
LanguagesConfig.clearLanguage(MultiLanguages.getApplication());
}

OnLanguageListener listener = MultiLanguages.getOnLanguagesListener();
if (listener != null) {
listener.onSystemLocaleChange(oldLocale, newLocale);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/java/com/hjq/language/MultiLanguages.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* time : 2019/05/03
* desc : 语种切换框架
*/
@SuppressWarnings("unused")
public final class MultiLanguages {

/** 应用上下文对象 */
Expand Down

0 comments on commit be92a8f

Please sign in to comment.