I am a new learner for Kotlin and I am trying to make a setting activity for my app and I want this activity to pass it's values to other activities. I tried different codes but none of them works and I tried to make a shared preference file but I don't know how to write the code
to be clear, I want to pass the font types from setting [main] activity to another activity but i don't know how!
my Main Activity
val preferences = applicationContext.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
val prefEditor = preferences.edit()
val fonts = arrayOf("Data1", "Data2", "Data3", "Data4")
val adapterCountry = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fonts)
val spinner = findViewById(R.id.spinner) as Spinner
spinner.adapter = adapterCountry
spinner.setSelection(preferences.getInt("position", 0))
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
spinner.setSelection(position)
prefEditor.putInt("position", position)
prefEditor.apply()
val selecteditem = parent.getItemAtPosition(position).toString()
if (selecteditem == "Data1"){
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
}
and this is my main 2 activity:
class Main2Activity : AppCompatActivity() {
internal lateinit var sh : SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
sh = PreferenceManager.getDefaultSharedPreferences(this)
}
override fun onStart() {
super.onStart()
if (sh.getBoolean("positon", false)){
when(sh.getInt("position", 0)){
0->{
t1.typeface = Typeface.createFromAsset(assets, "andlso.ttf")
}
1->{
t1.typeface = Typeface.createFromAsset(assets, "frsspbl")
}
}
}
}
i found the solution of this question here it is enter link description here