How to pass activity values to another activity (Kotlin)

How to pass activity values to another activity (Kotlin)



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





Try rewrite this answear to Kotlin.
– Hubert Bossy
Aug 26 at 21:58





If it's just from that activity to the other and theyre linked, consider just passing an intent extra. Otherwise if you do need it app-wide using preferences is a good way: developer.android.com/training/data-storage/shared-preferences
– P Fuster
Aug 26 at 23:54




2 Answers
2



So basically you want to pass data (font type) in Int from an activity to another one. You can use SharedPrefenrences but it isn't recommended in Android.


Int


SharedPrefenrences



I give you 2 solutions:



1. Using bundle



MainActivity.kt


class MainActivity : AppCompatActivity()

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

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.onItemSelectedListener = object : AdapterView.OnItemSelectedListener

override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long)
spinner.setSelection(position)
val selecteditem = parent.getItemAtPosition(position).toString()
if (selecteditem == "Data1")



// Start another activity with position
val intent = Intent(this@MainActivity, Main2Activity::class.java)
intent.putExtra("position", position);
startActivity(intent)


override fun onNothingSelected(parent: AdapterView<*>)






Main2Activity.kt


class Main2Activity : AppCompatActivity()

override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)

val position = intent.getIntExtra("position", 0)




2. Using SharedPreferences



MainActivity.kt


class MainActivity : AppCompatActivity()

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

val preferences = PreferenceManager.getDefaultSharedPreferences(this)

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)
val selecteditem = parent.getItemAtPosition(position).toString()
if (selecteditem == "Data1")



// Save position to prefs.
preferences.edit()
.putInt("position", position)
.apply()


override fun onNothingSelected(parent: AdapterView<*>)






Main2Activity.kt


class Main2Activity : AppCompatActivity()

override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)

// Get position from prefs.
val position = PreferenceManager.getDefaultSharedPreferences(this).getInt("position", 0)






Glad to hear that. Happy coding :)
– Son Truong
Aug 27 at 3:34



Intent has already existing functionality to send information from one activity to another.


Intent


// in your first activity:

val intent = Intent(context, Main2Activity::class.java)
.putExtra("position", position)
startActivity(intent)

// in your second activity, you can fetch the extras like this:

class Main2Activity : AppCompatActivity()

override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
val position = intent.getIntExtra("position", -1)





I don't recommend using shared preferences to pass events between screens because you can get stuck in awkward states if your app crashes before it can cleanup it's shared pref state.





it said that context doesn't have a classifier
– Abdulrhman Alanazi
Aug 27 at 1:59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)