[Android Studio|Kotlin] Activity 
Published: 2022-02-22 07:16:01

**1) Running sub activity from main activity** 
MainActivity(Button android:id="@+id/btnStart), SubActivity

[MainActivity.kt]

```
class MainActivity : AppCompatActivity() {

    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.btnStart.setOnClickListener{
            val intent = Intent(this, SubActivity::class.java)
            startActivity(intent)
        }
    }
}
```

**2) Sending and receiving activity values**
**2-1) Pass values ​​from main to sub.** 

[MainActivity.kt] 
```
class MainActivity : AppCompatActivity() {

    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.btnStart.setOnClickListener{
            val intent = Intent(this, SubActivity::class.java)
            intent.putExtra("from1", "hello Bundle")
            intent.putExtra("from2", 2020)
            startActivity(intent)
        }
    }
}
```

[activity_sub.xml ]

```
TextView android:id="@+id/to1"
TextView android:id="@+id/to2"
```
[SubActivity.kt] 

```
class SubActivity : AppCompatActivity() {
    val binding by lazy { ActivitySubBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        with(binding) {
            to1.text = intent.getStringExtra("from1")
            to2.text = "${intent.getIntExtra("from2", 0) }"
        }
    }
}
```

**2-2) Return the value to MainActivity.kt while exiting SubActivity.kt**
https://developer.android.com/training/basics/intents/result
**_registerForActivityResult()_** _* startActivityForResult is deprecated_


[MainActivity.kt] 

```
class MainActivity : AppCompatActivity() {

    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            if (result.resultCode == Activity.RESULT_OK) {
                val intent = result.data
                intent?.getStringExtra("returnValue")?.let { message ->
                    Toast.makeText(this, message, Toast.LENGTH_LONG).show()
                }
            }
        }
        binding.btnStart.setOnClickListener{
            val intent = Intent(this, SubActivity::class.java)
            intent.putExtra("from1", "hello Bundle")
            intent.putExtra("from2", 2020)
            startForResult.launch(intent) 
        }
    }
}
```
[activity_main.xml]

```
<androidx.constraintlayout.widget.ConstraintLayout
TextView android:id="@+id/textView2"
Button android:id="@+id/btnStart"
<androidx.constraintlayout.widget.ConstraintLayout
``` 

[SubActivity.kt] 

```
class SubActivity : AppCompatActivity() {
    val binding by lazy { ActivitySubBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        with(binding) {
            to1.text = intent.getStringExtra("from1")
            to2.text = "${intent.getIntExtra("from2", 0) }"
            btnClose.setOnClickListener{
                val returnIntent = Intent()
                returnIntent.putExtra("returnValue", editMessage.text.toString())
                setResult(Activity.RESULT_OK, returnIntent)
                finish()
            }
        }
    }
}
```
[activity_sub.xml]

```
<androidx.constraintlayout.widget.ConstraintLayout
TextView android:id="@+id/textView"
TextView android:id="@+id/to1"
TextView android:id="@+id/to2"
EditText android:id="@+id/editMessage"
Button android:id="@+id/btnClose"
<androidx.constraintlayout.widget.ConstraintLayout
```