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

Android 冻结应用程序的非活动片段类中的活动,android,kotlin,android-intent,android-activity,android-fragmentactivity,Android,Kotlin,Android Intent,Android Activity,Android Fragmentactivity,我正在使用Kotlin本地化我的Android应用程序,并希望每次按下单选按钮时都将内容设置为西班牙语。为此,每当单击按钮时,我都会启动一个LanguageActivity,它基本上应该将locale值设置为espanol。但是,我注意到,当LanguageActivity加载时,屏幕会变成白色,除非我按下后退按钮,否则不会发生任何事情。我也没有看到任何错误消息,所以我不确定发生了什么。我正在从非活动类NewsFragment中加载活动。我使用的是安卓API 30 另外,我对这一点非常陌生,如果

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

另外,我对这一点非常陌生,如果您能在
LanguageActivity
ContextUtils
中提供本地化逻辑的反馈,并提出更好的方法,我将不胜感激

新闻片段

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, 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, 0, -anchorView.height / 2, gravity)
    }
上下文

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
        }
    }


}

你不是出于这个目的才开始你的活动的

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

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

对于问题的这一部分,片段完全依赖于活动,因此它们不能在自身内部启动另一个活动

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
        }
    }


}