0

I want to know how to send 3 variables to another activity with intent in kotlin, I would like to know how the other intent should also be, not only the sending, but how it would have to receive it in the other activity activity with the 3 variables:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.yr.iolite.R

class RestrictionsActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_restrictions)
        var a = 10
        var b = 5
        var c = "2.5"
    }
}

activity where I want to receive it:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.yr.iolite.R

class home : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
    }
}
Pxnditx YR
  • 79
  • 1
  • 10
  • @Branddd I am not rude dude. I am just straightforward. If you are able to get solution after your R & D then it will be good but without doing anything or not to make your efforts then it will not be good things. Relating to this question there are thousand of answers get through google. I felt that without searching anything and post this question i commented as a _ordinary question_. – Piyush Nov 11 '20 at 12:29
  • okay, my bad i guess – Branddd Nov 11 '20 at 12:34

1 Answers1

1

just send data using putExtra();

  1. send

    class RestrictionsActivity : AppCompatActivity()
    {
     override fun onCreate(savedInstanceState: Bundle?)
     {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_restrictions)
         var a = 10
         var b = 5
         var c = "2.5"
    
         val nextIntent = Intent(this, home::class.java)
         nextIntent.putExtra("a", a)
         nextIntent.putExtra("b", b)
         nextIntent.putExtra("c", c)
         startActivity(nextIntent)
     }
    }
    
  2. recieve

    class home : AppCompatActivity()
    {
     override fun onCreate(savedInstanceState: Bundle?)
     {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_home)
    
         var intent = getIntent()
         var a = intent.getIntExtra("a",0)
         var b = intent.getIntExtra("b",0)
         var c = intent.getIntExtra("c",0)
     }
    }
    

UPDATE : add boolean type and String type

when you send data is same as IntType.

var a = 10
var b = "hello"
var c = true
nextIntent.putExtra("a", a)
nextIntent.putExtra("b", b)
nextIntent.putExtra("c", c)

but when you recieve data, use type for prevent NPE that can occur when the key is incorrect.

 var intent = getIntent()
 var a = intent.getIntExtra("a", 0)
 var b = intent.getStringExtra("b")
 var c = intent.getBooleanExtra("c",false)

maybe you can find more options in here

S T
  • 1,068
  • 2
  • 8
  • 16