Issue
I am making a virtual navigation app similar like google street view. I am taking users current location and their destination through spinners and then convert those inputs into strings. But now I want to use those inputs in my NavigationDispalyActivity.kt but I cannot figure out how to use intent properly.
My Codes
MainActivity.kt
package com.example.collegeroamer
import android.content.Intent
import android.os.Bundle
import android.widget.ImageButton
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var navigateButton: ImageButton
private lateinit var currentLocationSpinner: Spinner
private lateinit var destinationSpinner: Spinner
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigateButton = findViewById(R.id.navigateButton)
currentLocationSpinner = findViewById(R.id.currentLocationSpinner)
destinationSpinner = findViewById(R.id.destinationSpinner)
navigateButton.setOnClickListener {
val startingLocation = currentLocationSpinner.selectedItem.toString()
val destination = destinationSpinner.selectedItem.toString()
val intent = Intent(this, NavigationDisplayActivity::class.java)
intent.putExtra("selected_location", startingLocation)
startActivity(intent)
}
}
}
NavigationDisplayActivity.kt
@file:Suppress("UNUSED_EXPRESSION")
package com.example.collegeroamer
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class NavigationDisplayActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_display)
val locationImage = findViewById<ImageView>(R.id.locationImage)
val selectedLocation = intent.getStringExtra("selected_location")
val imageResourceId = getImageResourceIdForLocation(selectedLocation ?: "")
locationImage.setImageResource(imageResourceId)
}
private fun getImageResourceIdForLocation(location: String): Int {
return when (location) {
else -> R.drawable.default_image
}
}
}
Solution
You can pass multiple key-value pairs in an Intent. Each key must be unique or it will override whatever was set before with that key. For more info on Intents, you can read the developer documentation found here
You can do the following
//MainActivity
navigateButton.setOnClickListener {
val startingLocation = currentLocationSpinner.selectedItem.toString()
val destination = destinationSpinner.selectedItem.toString()
val intent = Intent(this, NavigationDisplayActivity::class.java)
intent.putExtra("selected_location", startingLocation)
intent.putExtra("destination", destination) //<--- This is the new change
startActivity(intent)
}
//NavigationDisplayActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_image_display)
val locationImage = findViewById<ImageView>(R.id.locationImage)
val selectedLocation = intent.getStringExtra("selected_location")
val imageResourceId = getImageResourceIdForLocation(selectedLocation ?: "")
locationImage.setImageResource(imageResourceId)
val destinationLocation = intent.getStringExtra("destination) //<-- Here we can fetch the value
}
Answered By - Bilal Naeem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.