단순 화면 이동 (+이제 데이터를 곁들인)

MainActivity.kt

binding.btnMove.setOnClickListener(View.OnClickListener {
            var intent = Intent(mContext, SecondActivity::class.java)
            intent.putExtra("text", binding.tvContent.text.toString())
            startActivity(intent)
        })

SecondActivity.kt

var intent = intent.getStringExtra("text")
        binding.tvText.text = intent
        Log.d(TAG, "onCreate: $intent")

Untitled

Untitled

데이터 응답받기 (+ 이제 deprecated 를 곁들인)

SecondActivity.kt

private const val TAG = "SecondActivity"
class SecondActivity : AppCompatActivity() {

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

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

        binding.btnMove.setOnClickListener(View.OnClickListener {
            Log.d(TAG, "onCreate: move clicked")
            var intent = Intent(this, ThirdActivity::class.java)
            ~~startActivityForResult~~(intent, 1000)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.~~onActivityResult~~(requestCode, resultCode, data)
        Log.d(TAG, "onActivityResult requestCode: $requestCode")
        Log.d(TAG, "onActivityResult resultCode: $resultCode")

        var getData = data?.extras?.get("data")
        Log.d(TAG, "onActivityResult data: $getData")
    }
}

ThirdActivity.kt

private const val TAG = "ThirdActivity"

class ThirdActivity : AppCompatActivity() {

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

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

        binding.btnMove.setOnClickListener(View.OnClickListener {
            var intent = Intent()
            intent.putExtra("data", "hello")
            setResult(RESULT_OK, intent)
            finish()
            }
        )
    }
}

화면 이동 버튼을 클릭하면 intent 실행

requestCode 를 들고 간다 → 누가 요청했는지 식별하기 위함