非活动片段类中的活动冻结了应用程序

问题描述

我正在使用Kotlin本地化我的Android应用程序,并希望每次按下单选按钮时将内容设置为西班牙语。为此,每当单击按钮时,我都会启动LanguageActivity,这基本上应该将语言环境值设置为espanol。但是,我注意到在加载LanguageActivity时,屏幕变成白色,除非按下后退按钮,否则什么也不会发生。我也没有看到任何错误消息,所以我不确定发生了什么。我正在从非活动NewsFragment类中加载活动。我正在使用Android API 30。

此外,我对此还很陌生,如果能在LanguageActivityContextUtils中提供有关本地化逻辑的反馈并提出任何更好的方法,将不胜感激。

新闻片段

internal fun showLanguagePopup(anchorView: View,gravity: Int) {
        val popupBinding = DataBindingUtil.inflate<ItemNewsLanguagePopupBinding>(layoutInflater,R.layout.item_news_language_popup,null,false)

        viewmodel.availableLanguages
            .forEachIndexed { index,language ->
                val button = layoutInflater.inflate(R.layout.item_news_language_popup_button,popupBinding.gLanguages,false) as RadioButton
                button.text = language.toNameString(activity!!)
                button.id = index
                button.isChecked = viewmodel.getCurrentLanguage() == language
                popupBinding.gLanguages.addView(button)
            }

        val popupWindow = PopupWindow(popupBinding.root,LinearLayout.LayoutParams.WRAP_CONTENT,true)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            popupWindow.elevation = 10f
        }

        popupBinding.bCancel.setonClickListener { popupWindow.dismiss() }
        popupBinding.bOk.setonClickListener {
            val selectedLanguageOrdinal = popupBinding.gLanguages.checkedRadioButtonId
            if (selectedLanguageOrdinal >= 0) {
                viewmodel.setupLanguage(viewmodel.availableLanguages[selectedLanguageOrdinal])
                val intent = Intent(activity,LanguageActivity::class.java)
                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
                drawerLogicView.onBackpressed()
            }
            popupWindow.dismiss()
        }

        popupWindow.showAsDropDown(anchorView,-anchorView.height / 2,gravity)
    }

语言活动

package com.oigetit.oigetit.ui.news.list

import android.content.Context
import android.content.Contextwrapper
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.oigetit.oigetit.utility.ContextUtils

import java.util.*


class LanguageActivity : AppCompatActivity() {


    override fun attachBaseContext(newBase: Context) {
        val localetoSwitchTo = Locale("es")
        val localeUpdatedContext: Contextwrapper? = ContextUtils.updateLocale(newBase,localetoSwitchTo)
        super.attachBaseContext(localeUpdatedContext)
    }
}

ContextUtils

package com.oigetit.oigetit.utility

import android.content.Context
import android.content.Contextwrapper
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import java.util.*


class ContextUtils(base: Context?) : Contextwrapper(base) {

    companion object {
        fun updateLocale(context: Context,localetoSwitchTo: Locale?): Contextwrapper? {
            var context = context
            val resources: Resources = context.resources
            val configuration: Configuration = resources.getConfiguration() // 1
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                val localeList = LocaleList(localetoSwitchTo) // 2
                LocaleList.setDefault(localeList) // 3
                configuration.setLocales(localeList) // 4
            } else {
                configuration.setLocale(localetoSwitchTo) // 5
            }
            //val resources: Resources = context.resources
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
                context = context.createConfigurationContext(configuration) // 6
            } else
            {
                context = context.createConfigurationContext(configuration)
            }
            return ContextUtils(context) // 8
        }
    }


}

解决方法

您并非以此意图开始您的活动。

startActivity(intent)行下方添加val intent = Intent(activity,LanguageActivity::class.java)

我正在从非活动NewsFragment类中加载活动,并且不想使用MainActivity进行加载

对于问题的这一部分,片段完全取决于活动,因此它们无法在自己内部开始其他活动。