Get string extra from activity Kotlin
AndroidKotlinAndroid Problem Overview
I want to get a string extra in another activity from an intent. This is the way to create my intent
val intent = Intent(this, Main2Activity::class.java)
intent.putExtra("samplename", "abd")
startActivity(intent)
How can i get the value of this intent in the another activity
Android Solutions
Solution 1 - Android
Answer found, in the next activity, you have to do this to get the string:
val ss:String = intent.getStringExtra("samplename").toString()
Solution 2 - Android
LOAD
val value: String = txt_act_main.text.toString() // or just your string
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("value", value)
startActivity(intent)
//option 2 all inner classes should be implemented to Serializable
getIntent().putExtra("complexObject", clickedTitle);
GET
var bundle :Bundle ?=intent.extras
var message = bundle!!.getString("value") // 1
var strUser: String = intent.getStringExtra("value") // 2
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
//option 2
var myProg = intent.getSerializableExtra("complexObject") as MenuModel
IMPLICIT (To Share with other apps)
val value: String = txt_act_main.text.toString()
var intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT, value)
intent.type="text/plain"
startActivity(Intent.createChooser(intent,"Share to "))
Solution 3 - Android
Can use this code :
val bundle = intent.extras
var sampleName: String = ""
if (bundle != null) {
sampleName = bundle.getString("samplename")
}
Solution 4 - Android
you can check if the intent value is null or not
val bundle :Bundle ?=intent.extras
if (bundle!=null){
val message = bundle.getString("object") // 1
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
Solution 5 - Android
The accepted answer doesn't solve the case where the intent is not there. Because when the key is not exist in intent, getStringExtra()
will give you null
even its signature indicates a String
rather than a String?
.
You can use val text:String = intent.getStringExtra(intentKey) ?: ""
to make sure no NPE happened.
But one more answer here:
This is for the case where, you try to retrieve the string from intent, if the value is there, we get the value, otherwise, it will go back to the previous screen because this intent is critical. Something wrong will happen but we don't want to crash the activity.
private fun getStringFromIntentOrShowError(intentKey: String):String {
val text:String? = intent.getStringExtra(intentKey)
if (text == null) {
showDialog(
"Error",
"No $intentKey found"
) {
it.dismiss()
finish()
}
return ""
}
return text
}
// I use anko to show a dialog, you can use your one.
private fun showDialog(
title:String,
message:String,
yesButtonCallback: (d:DialogInterface) -> Unit
) {
alert(message, title){ yesButton{
yesButtonCallback(it)
} }.show()
}
Then you can use it like this:
val text:String = getStringFromIntentOrShowError("asd")
and text will always have a value
Solution 6 - Android
In Main2Activity you can have your code like this :
val intent = getIntent();
val myValue = intent.getStringExtra("key")
Log.d(TAG,"myValue"+myValue)
Solution 7 - Android
You can use this simple Kotlin Extension
fun Intent.getData(key: String): String {
return extras?.getString(key) ?: "intent is null"
}
this Extension checks if the intent value is null, if the value is null it will return intent is null otherwise, it will return the value
use it like this
val username = intent.getData("username")
Solution 8 - Android
First you need to initialize the intent variable. Then take out the data like we do in java :)
val intent: Intent = intent
var data = intent.getStringExtra("mydata")