javaandroidkotlinandroid-fragmentsandroid-activity

Is it possible to invoke a back button click programmatically?


I've seen plenty of answers explaining how to override and add functionality to a back button press via user's interaction, but almost none of the answers explain how to programmatically trigger a back button press without any user input. Answers that do address it give somewhat deprecated code samples that don't really compute.


Solution

  • From your activity just call => onBackPressed(),

    or form your fragment => activity?.onBackPressed()