How to Add and Customize Back Button of Action Bar in Android?
The action bar (sometimes referred to as the app bar), if it exists for an activity, will be at the top of the activity’s content area, typically directly underneath the status bar. It is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items that become visible when the user clicks the “menu” button. In general, an ActionBar composed of the following four components:
- App Icon: App branding logo or icon will be shown here
- View Control: A dedicated space to display the Application title. Also provides the option to switch between views by adding spinner or tabbed navigation
- Action Buttons: Major actions of the app could be added here
- Action Overflow: All unimportant action will be displayed as a menu
Below is a sample image to show where the Action Bar/Toolbar/App Bar is present on an android device.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
The action bar is a primary toolbar inside an activity that can be used to display an activity title and other interactive items. One of the most used items is a Back Navigation Button. The back button is used to move backward from the previously visited screen by the user. Most Android devices have a dedicated back button still a back button on the action bar enhances the user experience.
Источник
Android pressing the back button
Android Default Back Navigation & Customization
November 14, 2016 by Srinivas
Like web browser’s back button takes you through previously visited web pages, Android system back button moves you through previous screens of an active app. Like back button functionality in web browser is provided by browser, Android back button functionality is provided by Android system. Both of them load previous pages or screens from cache or stack on clicking or touching back button.
To provide back navigation for your app, you don’t need to do anything. In most cases default back navigation is sufficient to provide perfect back navigational experience to users.
Below you can see back button provided by android system, it takes you back to previously worked activities or screens of currently active app in reverse chronological order.
Android back button default behavior
Android uses back stack to keep track of worked-on screens in chronological order. Back button functionality uses back stack to show previous screens and state on pressing it. Android system creates a new back stack when user starts an app by launching main activity from home screen. If the app is already open but inactive, system brings it to foreground and uses existing stack to resume current activity or top activity in the stack.
When current activity opens a new activity, the new activity is placed on top of the stack. New activity becomes active and the activity which started the new activity becomes inactive and stays in the stack.
If current activity opens a new activity which is already in the stack, system creates a new instance of the new activity and places it on top of the stock.
Android allows applications to start components of other apps. When another application opens an activity in your app, the activity is placed in the stack of the app which started your app activity.
When user presses back button, current activity is removed from the stack and destroyed and previous activity becomes current from the stack. All the activities placed in a stack are considered as a task.
Why to customize back navigation
Default back button functionality may not be suitable in some scenarios. In these cases, you may have to customize back button functionality to provide consistent navigation experience to users. Here are two scenarios where you need to provide custom back button functionality.
One of the best features of android is allowing multiple entries meaning even though there needs to be one main activity which is instantiated when app icon is pressed, app can be entered into by starting any activity in your app. For example, users can come to your app from app widget or notifications and navigate directly to a deep screen in the app hierarchy. In this scenario, default back button flow may not provide best user experience.
Apps can embed web browser on screens. When user navigates in the embedded browser and visits few web pages, you may want to change the default android back button behavior. Instead of taking user to previous screen, you may want it to navigate back through browser historical pages and then default behavior of going back to previously visited screens.
How to enhance or override android default back navigation
I am going to provide code for two scenarios in which you may need to provide different back navigation behavior than default one.
Scenario 1:
User starts an activity in a separate stack, from notification or widget, which is deep in app hierarchy. In this case, default back button exits the app as it is a new stack without previous screens. Instead of exiting the app, taking the user to either main screen or starting screen of current flow gives user an opportunity to continue using the app. That means you need to add activities to back stack.
Below is the code to show above scenario and implementation. The sample application has three screens, MainActivity screen, ViewCoupon screen and ViewStoreCoupons screen. MainActivity has a button to send notification. The method which handles the click event sets the parent stack for the Intent which is started when user clicks notification. Notice that parent activity (ViewStoreCoupons) is configured for ViewCoupon activity in AndroidManifest.xml file.
Below is the flow to test the sample app.
- Start the app
- Click “send notification” button on main screen
- View notification by dragging notification drawer
- Click notification, it opens ViewCoupon activity in separate stack
- Click back button, it takes you to parent activity , ViewStoreCoupons activity , instead of existing the app
View coupons activity
View all coupons activity
Scenario 2:
You need to override onBackPressed method of the activity which embeds browser on the screen.
About
Android app development tutorials and web app development tutorials with programming examples and code samples.
Источник