2
class FirstActivity : AppCompatActivity() {
    companion object{
        val USER_KEY="FirstActivity"
    }


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

        button_firstActivity.setOnClickListener {
            val string:String=textView_first.text.toString()

            val intent=Intent(this,MainActivity::class.java)

            intent.putExtra(USER_KEY,string)
            startActivity(intent)
        }
    }
}


class MainActivity : AppCompatActivity() {
    companion object{
        val MAINUSERKEY="MainActivity"
        var str:String=""
    }


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

        str=intent.getStringExtra(FirstActivity.USER_KEY)

        textview_main.text=str

        button_Run.setOnClickListener {
            val edittextstring=editText1.text.toString()
            val intent=Intent(this,MainActivity::class.java)
            intent.putExtra(MAINUSERKEY,edittextstring)
            startActivity(intent)
        }
    }
}

Hello every one! I am new to Android programming with Kotlin.

I have two activities, suppose A and B. I want to start activity B from A and when B starts, it will display the TextView string of A into TextView_Main.

It is working fine now. I want to start activity B again on clicking button_Run which is on Activity B and passing a string again which I entered in edittext of Activity B. And now it should be displayed on textview of Activity B, when it opens again.

Please help me do this.

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51

2 Answers2

0

The problem is that the edittext string is being stored as an intent extra with name MAINUSERKEY="MainActivity”, which is different from the extra you are currently extracting on your MainActivity, the one with name USER_KEY="FirstActivity”. So I would do something like this to ensure I get the correct string extra:

str = with(intent) {
     getStringExtra(FirstActivity.USER_KEY) ?: getStringExtra(MainActivity.MAINUSERKEY) ?: "No string extra was found"
}
  • Thanks @JaviHerAr its like a switch (when) statement and i hope it will work.... – Vikas Rathore Nov 01 '19 at 04:07
  • I am glad to have been of help. Just to clarify, `with` function is not like a switch/when, it allows you to execute certain code over the object you pass as an argument inside the lambda, in this case the `intent`. You can do it without using the `with` function, it would be like this: `str = intent.getStringExtra(FirstActivity.USER_KEY) ?: intent.getStringExtra(MainActivity.MAINUSERKEY) ?: "No string extra was found"` [Here](https://kotlinlang.org/docs/reference/scope-functions.html#with) you have a detailed explanation of it, and other scope functions that you can use in Kotlin. – Javier Herrero Nov 01 '19 at 16:56
  • By the way, could you please mark my answer as valid answer? Thanks! – Javier Herrero Nov 01 '19 at 17:01
0

it is more clear to startActivity like code below, add this code in ActivityB

   companion object{

    private const val EXTRA_ MAIN_USERKEY = "EXTRA.MAIN_USERKEY"

    fun getIntent(context:Context, userKey:String): Intent
    {
        val intent = Intent(context,ActivityB::class.java)
        intent.putExtra(EXTRA_ MAIN_USERKEY, userKey)
        return intent
    }
}

and this code in ActivityA:

startActivity(ActivityB.getIntent(this,"some key"))

so every time you start activityB you should pass the string

Golnaz
  • 19
  • 5