Skip to content

Instantly share code, notes, and snippets.

@LucasFebatis
Last active September 17, 2023 02:32
Show Gist options
  • Save LucasFebatis/ae9cb85a5646384bafbe0b90b4e76c53 to your computer and use it in GitHub Desktop.
Save LucasFebatis/ae9cb85a5646384bafbe0b90b4e76c53 to your computer and use it in GitHub Desktop.
ActionBar com o botão voltar usando o Material 3
package com.example.myapplication
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Tornando o botão voltar da ActionBar visivel
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
// Adicionando ação para finalizar a activity quando apertar o botão voltar
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
finish()
return true
}
}
return super.onOptionsItemSelected(item)
}
}
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MyApplication" parent="Theme.Material3.DayNight">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.MyApplication" parent="Base.Theme.MyApplication" />
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment