In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, it doesn't do that. How do I make that icon take you back to the previous screen?
Android – Back button in the title bar
androidthemestitlebaruser interface
Related Topic
- Android – How to stop EditText from gaining focus at Activity startup in Android
- Android – the difference between “px”, “dip”, “dp” and “sp”
- Android – Is quitting an application frowned upon
- Android – How to hide the title bar for an Activity in XML with existing custom theme
- Android – unique Android device ID
- Android – How to change an Android app’s name
- Java – Proper use cases for Android UserManager.isUserAGoat()
Best Answer
There are two simple steps to create a back button in the title bar:
First, make the application icon clickable using the following code in the activity whose title bar you want to have a back button in:
After you have added the above code, you will see a back arrow appear to the left of the application icon.
Second, after you have done the above, you still have to create code that will take advantage of the click event. To do so, be aware that, when you actually click on the application icon, an
onOptionsItemSelected
method is called. So to go back to the previous activity, add that method to your activity and putIntent
code in it that will return you to the previous activity. For example, let's say the activity you are trying to go back to is calledMyActivity
. To go back to it, write the method as follows:That's it!
(In the Android developers API, it recommends messing around with the manifest and adding stuff like
android:parentActivityName
. But that doesn't seem to work for me. The above is simpler and more reliable.)And in your Activity