Reputation: 2524
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
Upvotes: 69
Views: 153209
Reputation: 17131
Can use this code :
val bundle = intent.extras
var sampleName: String = ""
if (bundle != null) {
sampleName = bundle.getString("samplename")
}
Upvotes: 7
Reputation: 2524
Answer found, in the next activity, you have to do this to get the string:
val ss:String = intent.getStringExtra("samplename").toString()
Upvotes: 89
Reputation: 820
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")
Upvotes: 2
Reputation: 6617
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 "))
Upvotes: 29
Reputation: 2864
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")
Upvotes: 2
Reputation: 99
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()
}
Upvotes: 6
Reputation: 3769
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
Upvotes: 5
Reputation: 420
In Main2Activity you can have your code like this :
val intent = getIntent();
val myValue = intent.getStringExtra("key")
Log.d(TAG,"myValue"+myValue)
Upvotes: 5