设备(1)
This commit is contained in:
@@ -1,38 +1,18 @@
|
||||
package com.example.defenseapplication.base
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.example.defenseapplication.utils.LanguageUtil
|
||||
import com.example.defenseapplication.utils.ActivityManager
|
||||
|
||||
/**
|
||||
* 支持语言切换的 Activity 基类
|
||||
* 所有需要支持多语言的 Activity 都应该继承此类
|
||||
*/
|
||||
abstract class BaseActivity : AppCompatActivity() {
|
||||
|
||||
override fun attachBaseContext(newBase: Context) {
|
||||
// 在 Activity 创建前应用语言设置
|
||||
val context = LanguageUtil.attachBaseContext(newBase)
|
||||
super.attachBaseContext(context)
|
||||
}
|
||||
open class BaseActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// 确保语言设置被应用
|
||||
LanguageUtil.updateResources(this)
|
||||
ActivityManager.addActivity(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换语言
|
||||
* 调用后会重新创建 Activity 以应用新语言
|
||||
*/
|
||||
protected fun switchLanguage(language: String) {
|
||||
val currentLang = LanguageUtil.getCurrentLanguage(this)
|
||||
if (currentLang != language) {
|
||||
LanguageUtil.switchLanguage(this, language)
|
||||
// 重新创建 Activity 以应用新语言
|
||||
recreate()
|
||||
}
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
ActivityManager.removeActivity(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user