Search code examples
javaandroidbuttonback

Software back button android


I need a back button with same functionality as default back button of device but inside of app.


Solution

  • Android activities are stored in the activity stack. You can go back by just finishing the current activity:

    add an onClick listener to your button in the activity xml like this:

     <Button
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/self_destruct"
         android:onClick="selfDestruct" />
    

    then create a method in your activity with the same name you specified in the xml

     public void selfDestruct(View view) {
         finish()
     }
    

    You can read more in the docs.