在 Kotlin 中关闭应用程序后,如何将 onItemSelected 保留在所选的同一项目上?

How to keep onItemSelected on the same item selected after closing the app in Kotlin?

我正在用 Kotlin 翻译一个应用程序,用户可以选择我创建的两种不同语言 Activity:

@Suppress("DEPRECATION")
class Languages_Activity : AppCompatActivity() {
lateinit var spinner: Spinner
lateinit var locale: Locale
var back_btn: LinearLayout? = null
private var currentLanguage = "en"
private var currentLang: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_languages_)
    title = "KotlinApp"
    currentLanguage = intent.getStringExtra(currentLang).toString()
    spinner = findViewById(R.id.spinner)
    val list = ArrayList<String>()
    list.add("Select Language")
    list.add("English")
    list.add("Malay")

    val adapter = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, list)
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinner.adapter = adapter
    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            when (position) {
                0 -> {
                }
                1 -> setLocale("en")
                2 -> setLocale("my")

            }
        }
        override fun onNothingSelected(parent: AdapterView<*>) {}

    }
    back_btn = findViewById(R.id.back_btn_language)

    back_btn?.setOnClickListener {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }
}
private fun setLocale(localeName: String) {
    if (localeName != currentLanguage) {
        locale = Locale(localeName)
        val res = resources
        val dm = res.displayMetrics
        val conf = res.configuration
        conf.locale = locale
        res.updateConfiguration(conf, dm)
        val refresh = Intent(
            this,
            Languages_Activity::class.java
        )
        refresh.putExtra(currentLang, localeName)
        startActivity(refresh)
    } else {
        Toast.makeText(
            this@Languages_Activity, "Language, , already, , selected)!", Toast.LENGTH_SHORT).show();
    }
}
override fun onBackPressed() {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_HOME)
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
    startActivity(intent)
    finish()
    exitProcess(0)
}
}

当我选择一种语言时,该应用程序将显示所选语言的值,但是当我关闭该应用程序并再次 运行 它时,选择将重置

即使在关闭应用程序并再次 运行 之后,如何保持项目被选中?

你可以使用shared preferences

对于这个用例。

当您想要存储可以在应用程序关闭和打开时保留数据的键值对时,共享首选项很有用。

仅当您在设置中清除应用数据或卸载应用时,此数据才会被删除

在您的 onCreate 中,您可以添加此代码段

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
val selectedLanguageIndex = sharedPref?.getInt("LANGUAGE_SELECTED", 0)?:0
spinner.setSelection(selectedLanguageIndex)

在你的 onItemSelected

val sharedPref = requireActivity().getPreferences(Context.MODE_PRIVATE)
with (sharedPref.edit()) {
    putInt("LANGUAGE_SELECTED", position)
    apply()
}

kotlin 有共享偏好系统。 当您必须记住一些选项直到更改时,它会有所帮助。