Search code examples
androidkotlinandroid-intentnumberpicker

Passing multiple values to another Activity in Android Studio with Kotlin


Passing multiple values to another activity is not working in my android project. I want to create setting menu activity that passes setting values of the application in android. Those values are set by widgets such as switch and number picker.

this is my MenuActivity witch passes vlues from

const val EXTRA_VALUE1="com.exapmple.MyApplication.SETTING"
const val EXTRA_VALUE2="com.exapmple.MyApplication.SETTING"
const val EXTRA_VALUE3="com.exapmple.MyApplication.SETTING"
const val EXTRA_VALUE4="com.exapmple.MyApplication.SETTING"

class MenuActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.example.bowlingo.R.layout.activity_menu)

        var value1 = 0
        var value2 = 1
        var value3 = 1

        val switch1: Switch = findViewById(R.id.switch1)
        switch1.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked)
                value1 = 1
        }
        val switch2: Switch = findViewById(R.id.switch2)
        switch2.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked)
                value2 = 0
        }
        val switch3: Switch = findViewById(R.id.switch3)
        switch3.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked)
                value3 = 0
        }

        val numPicker = findViewById<NumberPicker>(R.id.numPicker)
        numPicker.wrapSelectorWheel = false
        val values = arrayOf("3", "5", "7")
        numPicker.minValue = 0
        numPicker.maxValue = values.size-1
        numPicker.displayedValues = values
        val vlue4= numPicker.value

        val start: Button = findViewById(R.id.start)
        start.setOnClickListener { // Handler code here.
            val intent = Intent(this@MenuActivity, MainActivity::class.java).apply {
                    putExtra(EXTRA_VALUE1, value1)
                    putExtra(EXTRA_VALUE2, value2)
                    putExtra(EXTRA_VALUE3, value3)
                    putExtra(EXTRA_VALUE4, value4)
            }
            startActivity(intent);
        }
    }
}

This is a part of the MainActivity gets the values

class MainActivity : AppCompatActivity() {

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

        val value1 = intent.getIntExtra(EXTRA_VALUE1,0)
        val value2 = intent.getIntExtra(EXTRA_VALUE2,0)
        val value3 = intent.getIntExtra(EXTRA_VALUE3,0)
        val value4 = intent.getIntExtra(EXTRA_VALUE4,0)

        val textView = findViewById<TextView>(R.id.textView).apply {
            text = value1.toString() +":" + value2.toString() +":" + value3.toString()+ ":" + value4.toString()

        }
}

the textView on the MainAtivity always shows"1:1:1:1" and it doesn't change if I tap the switches and nuberpicker. Could anyone help me please.


Solution

  • You need to have a different value for each constant. because right now you're only receiving the value from EXTRA_VALUE1

    for example:

    const val EXTRA_VALUE1="com.exapmple.MyApplication.SETTING_1"
    const val EXTRA_VALUE2="com.exapmple.MyApplication.SETTING_2"
    const val EXTRA_VALUE3="com.exapmple.MyApplication.SETTING_3"
    const val EXTRA_VALUE4="com.exapmple.MyApplication.SETTING_4"