In questo articolo vedremo come inserire un pulsante nell'ActionBar per tornare all'activity precedente.
Per fare questo occorre aprire l'Activity in cui vogliamo inserire il pulsante in questo caso il file SecondActivity.kt e inserire queste righe di codice all'interno della funzione onCreate.
Poi fuori dalla funzione onCreate occorre inserire queste righe di codice per gestire il click su questo pulsante
Ecco come si presenta il file SecondActivity.kt
Per fare questo occorre aprire l'Activity in cui vogliamo inserire il pulsante in questo caso il file SecondActivity.kt e inserire queste righe di codice all'interno della funzione onCreate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Actionbar per tornare indietro | |
val actionbar = supportActionBar | |
//Impostazione titolo dell'ActionBar | |
actionbar!!.title = resources.getString(R.string.app_name) | |
//attivazione del tasto Indietro nell'ActionBar | |
actionbar.setDisplayHomeAsUpEnabled(true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//gestione del clic del pulsante per tornare indietro | |
override fun onSupportNavigateUp(): Boolean { | |
onBackPressed() | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dm.tutorialbackactivity | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
class SecondActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_second) | |
//Actionbar per tornare indietro | |
val actionbar = supportActionBar | |
//Impostazione titolo dell'ActionBar | |
actionbar!!.title = resources.getString(R.string.app_name) | |
//attivazione del tasto Indietro nell'ActionBar | |
actionbar.setDisplayHomeAsUpEnabled(true) | |
} | |
//gestione del clic del pulsante per tornare indietro | |
override fun onSupportNavigateUp(): Boolean { | |
onBackPressed() | |
return true | |
} | |
} |
Download Project | ![]() |
Download file APK | ![]() |
Visualizza su | ![]() |
Commenti
Posta un commento