Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go to another activity on Menu Item selection in kotlin

Tags:

kotlin

menu

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
            }
        }
like image 263
Ali Ghassan Avatar asked Nov 15 '25 20:11

Ali Ghassan


2 Answers

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)

    }
like image 53
Ali Ghassan Avatar answered Nov 17 '25 10:11

Ali Ghassan


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)
}
like image 21
Sam Tomashi Avatar answered Nov 17 '25 09:11

Sam Tomashi