Skip to content

Instantly share code, notes, and snippets.

@mikkipastel
Created September 26, 2022 07:23
Show Gist options
  • Save mikkipastel/46cda256101726bb2d346085e783c19f to your computer and use it in GitHub Desktop.
Save mikkipastel/46cda256101726bb2d346085e783c19f to your computer and use it in GitHub Desktop.
activity & fragment workshop : send EditText to show text of TextView at SecondActivity
package com.ascedncorp.androidbasic
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.ascedncorp.androidbasic.databinding.ActivityMainBinding
class MainActivity: AppCompatActivity() {
private val binding: ActivityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("text", binding.edittext.text.toString())
startActivity(intent)
}
}
}
package com.ascedncorp.androidbasic
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.ascedncorp.androidbasic.databinding.ActivitySecondBinding
class SecondActivity: AppCompatActivity() {
private val text by lazy {
intent.getStringExtra("text")
}
private val binding: ActivitySecondBinding by lazy {
ActivitySecondBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.textview.text = text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment