class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//var music=Music(1, "回梦游仙")
var musicpl=MusicParce(1, "桃花岛 ")
val intent = Intent(this@MainActivity, JumpActivity::class.java)
intent.putExtra("musicbean", musicpl)
startActivity(intent)
}
}
3. get方式变化
class JumpActivity : AppCompatActivity() {
val TAG="JumpActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_jump)
var music=intent.getParcelableExtra("musicbean") as MusicParce?
if (music != null) {
music.musictitle?.let {
Log.e(TAG, it)
}
}
}
}
四:使用Bundle
原理就将多个东西打包,先封在Bundle,再将其用intent传递
//使用Bundle
val bundle = Bundle()
val intent = Intent(this@MainActivity, JumpActivity::class.java)
//设置数据
//设置数据
val id = 3
val musictitle = "夏鱼"
//把数据保存到Bundle里
//把数据保存到Bundle里
bundle.putInt("musicid", id)
bundle.putString("musictitle", musictitle)
//把bundle放入intent里
//把bundle放入intent里
intent.putExtra("Message", bundle)
startActivity(intent)
取出来
val bundle = intent.getBundleExtra("Message")
//获取数据
//获取数据
val id = bundle!!.getInt("musicid")
val musictitle = bundle!!.getString("musictitle")
Log.e(TAG, "${id}+${musictitle}")