68

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

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
zasaz
  • 2,404
  • 2
  • 15
  • 27

8 Answers8

86

Answer found, in the next activity, you have to do this to get the string:

val ss:String = intent.getStringExtra("samplename").toString()
SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44
zasaz
  • 2,404
  • 2
  • 15
  • 27
29

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 "))
Samir
  • 6,405
  • 5
  • 39
  • 42
7

Can use this code :

val bundle = intent.extras
var sampleName: String = ""
if (bundle != null) {
    sampleName = bundle.getString("samplename")
}
Antonio
  • 240
  • 3
  • 15
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
6

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()

    }
Rahul ShaRma
  • 99
  • 4
  • 8
5

In Main2Activity you can have your code like this :

    val intent = getIntent();
    val myValue = intent.getStringExtra("key")
    Log.d(TAG,"myValue"+myValue)
kukroid
  • 420
  • 6
  • 15
5

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

Albert Gao
  • 3,653
  • 6
  • 40
  • 69
2

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")
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
2

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")