Home > other >  Input value not updating in another activity which I pass in Intents
Input value not updating in another activity which I pass in Intents

Time:10-29

Mine Main Activity :-

  var sec  =  second_activity()
  var button1: Button? = null
  var button2: Button? = null
  var button3: Button? = null
  var button4: Button? = null



button1 = findViewById<View>(R.id.button1) as Button
button2 = findViewById<View>(R.id.button2) as Button
button3 = findViewById<View>(R.id.button3) as Button
button4 = findViewById<View>(R.id.button4) as Button




button1?.setOnClickListener {
    sec.input = "a"
    val intent: Intent = Intent(this, second_activity::class.java)
    startActivity(intent)

}
button2?.setOnClickListener {
    sec.input = "b"
    val intent: Intent = Intent(this, second_activity::class.java)
    startActivity(intent)

}

Second activity :-

class second_activity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var toolbar: Toolbar
    private lateinit var mDrawerLayout: DrawerLayout
    var input : String = " "


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

 
        //getting recyclerview from xml
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        //adding a layoutmanager
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        //crating an arraylist to store users using the data class user
        val users = ArrayList<User>()
        //adding some dummy data to the list
        //creating our adapter
        val adapter = CustomAdapter(this , users)
        //now adding the adapter to recyclerview
        recyclerView.adapter = adapter

        Toast.makeText(this, input , Toast.LENGTH_SHORT).show()

        when(input) {

            "a" ->  {
                users.add(User(R.drawable.bc))
            }
            "b" -> {
                users.add(User(R.drawable.bc))
                users.add(User(R.drawable.bc))
                users.add(User(R.drawable.bc))
                users.add(User(R.drawable.bc))
            }

        }



    }

Input value is not updating which I pass from MainActivity. It is always taking the blank value which are present in second activity. I also tried by changing the position of Input. but not worked. please help

Is there any other way to find which button is clicked in MainActivity from Second Activity

CodePudding user response:

Yes, there is another way. Use intent.putExtra("requestCode", requestCode). Then in the second activity you can get that requestCode with getIntent().getExtra().getInt("requestCode").

CodePudding user response:

Yes, you can pass the values from one activity to another activity using the following code,

val intent: Intent = Intent(this, SecondActivity::class.java)
intent.putExtra(name, value)
startActivity(intent)

I have given sample code below, please try this..

In first activity, I send the "language" value.

val intent: Intent = Intent(this, SecondActivity::class.java)
intent.putExtra("language", "tamil")
startActivity(intent)

In second activity, I retrived the "language" value.

Put this code in your second activity onCreate()

val intent: Intent = getIntent()
var language = ""
if (intent != null) {
   language = intent.getStringExtra("language")
 }
 println("Language : $language")
  • Related