Friday, 23 April 2021

setting language before setContentView results in unending loop

I have a fragment with settings of an app that saves the user selected mode and language ( a bit of code:

        binding.languageButton.setOnClickListener{
            builder = AlertDialog.Builder(requireContext())
            builder.setTitle(getString(R.string.setLanguage))
            builder.setItems(langArray) { _, which ->
                
                val sharedPref = requireActivity().getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
                sharedPref.putString("LANGUAGE", langArray[which]).apply()
                checkUserPreferences()

                changeLanguage(langArray[which])
                binding.languageButton.text = langArray[which]
            }
            builder.create()
            builder.show()
        }
    }

    private fun changeLanguage(language: String) {
        if(language != binding.languageButton.text.toString()){
            val local = Locale(language)
            val dm =  resources.displayMetrics
            val con = resources.configuration
            con.locale = local
            resources.updateConfiguration(con, dm)
            val refresh = Intent(
                    requireContext(),
                    MainActivity::class.java
            )
            refresh.putExtra(binding.languageButton.text.toString(), language)
            startActivity(refresh)
        }
    }

and that part (as mentioned) saves mode and selected language to sharedPreferences that I later want to use in mainActivity and other fragments, and I've put in MainActivity:

    private fun loadPreferences(){
        val preferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE)
        Log.i(TAG, preferences.getString("LANGUAGE", "eng").toString())
        Log.i(TAG, preferences.getInt("MODE", 1).toString())
        val local = Locale(preferences.getString("LANGUAGE", "eng").toString())
        val dm =  resources.displayMetrics
        val con = resources.configuration
        con.locale = local
        resources.updateConfiguration(con, dm)
        val refresh = Intent(
                this.baseContext,
                MainActivity::class.java
        )
        refresh.putExtra(preferences.getString("LANGUAGE", "eng").toString(),
                preferences.getString("LANGUAGE", "eng"))
        startActivity(refresh)
    }

and this is referenced in:

class MainActivity : AppCompatActivity() {
    // TODO: IT'S THE ONE
    companion object {
        private const val TAG = "MainActivity"
        private const val CHANNEL_ID = "plantz_app_channel_01"
        private const val notificationId = 909
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadPreferences()
        setContentView(R.layout.activity_main)

// ...

and after running the app It only shows the app logo and logs sharedPreferences but the app doesn't go any further, I've tried to tweak with it for a bit but It hasn't done much, any ideas what should I change to make it work?

Thanks in advance :)



from setting language before setContentView results in unending loop

No comments:

Post a Comment