import java.util.Random
fun main(args: Array<String>) {
val random = Random()
val num = random.nextInt(4)
val stars = arrayOf("★☆☆☆☆", "★★☆☆☆", "★★★☆☆", "★★★★☆", "★★★★★")
val rand_stars = stars.get(num)
print("나의 Today's LUCK:" + rand_stars)
}
- 도착 액티비티 입력시, activity::class.java 형식으로 입력 (subActivity.class가 아님)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val intent = Intent(this, subdActivity::class.java)
intent.putExtra("keyString", "Androidkey")
startActivity(nextIntent)
}
}
}
putExtra에는 다음과 같이 다양하게 전달 가능
2. 이동한 subActivity 수신
val bundle: Bundle? = intent.extras
val string: String? = intent.getString("keyString")
val myArray: ArrayList<String>? = intent.getStringArrayList("myArray")
val student = Student()
val intent = Intent(this, OtherActivity::class.java)
intent.putExtra("studentData", student)
startActivity(intent)
3) Serializable Data 전송
data class Blog(
val name: String = "Androidly",
val year: Int = 2018
) : Serializable
val blog = Blog("a", 1)
val intent = Intent(this, OtherActivity::class.java)
intent.putExtra("blogData", blog as Serializable)
startActivity(intent)