l added Options menu item Selected in toolbar of my app . l want add action on click on item even go to another activity . l Intent but does not work
override fun onOptionsItemSelected(item: MenuItem): {
when (item.itemId) {
R.id.flightarrbeforbgw ->
Intent intent = new Intent(this, FlightsArrivelBeforBGW.class);
this.startActivity(intent)
else ->
return null
}
}
l try with is code and his worked fine
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
//noinspection SimplifiableIfStatement
if (id == R.id.searchflights) {
val intent = Intent(this, FlightsArrivelBeforBGW::class.java)
this.startActivity(intent)
return true
}
if (id == R.id.flightarrbeforbgw) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show()
return true
}
if (id == R.id.flight_dep_list) {
Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show()
return true
}
return super.onOptionsItemSelected(item)
}
Your intent is not formatted correctly. This is how it's supposed to be:
Intent (this, YourActivity::class.java)
So your code should look like this:
when (item.itemId) {
R.id.flightarrbeforbgw ->{
this.startActivity(Intent(this,FlightsArrivelBeforBGW::class.java))
return true
}
else -> super.onOptionsItemSelected(item)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With