Android navigation
Providing Up Navigation
The up navigation will allow our application to move to previous activity from the next activity. It can be done like this.
To implement Up navigation, the first step is to declare which activity is the appropriate parent for each activity. You can do it by specifying parentActivityName attribute in an activity. Its syntax is given below −
android:parentActivityName = "com.example.test.MainActivity"
After that you need to call setDisplayHomeAsUpEnabled method of getActionBar() in the onCreate method of the activity. This will enable the back button in the top action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
The last thing you need to do is to override onOptionsItemSelected method. when the user presses it, your activity receives a call to onOptionsItemSelected(). The ID for the action is android.R.id.home.Its syntax is given below −
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } }
Handling device back button
Since you have enabled your back button to navigate within your application, you might want to put the application close function in the device back button.
It can be done by overriding onBackPressed and then calling moveTaskToBack and finish method. Its syntax is given below −
@Override public void onBackPressed() { moveTaskToBack(true); MainActivity2.this.finish(); }
Apart from this setDisplayHomeAsUpEnabled method, there are other methods available in ActionBar API class. They are listed below −
| Sr.No | Method & description |
|---|---|
| 1 |
addTab(ActionBar.Tab tab, boolean setSelected)
This method adds a tab for use in tabbed navigation mode
|
| 2 |
getSelectedTab()
This method returns the currently selected tab if in tabbed navigation mode and there is at least one tab present
|
| 3 |
hide()
This method hide the ActionBar if it is currently showing
|
| 4 |
removeAllTabs()
This method remove all tabs from the action bar and deselect the current tab
|
| 5 |
selectTab(ActionBar.Tab tab)
This method select the specified tab
|
Example
The below example demonstrates the use of Navigation. It crates a basic application that allows you to navigate within your application.
To experiment with this example, you need to run this on an actual device or in an emulator.
| Steps | Description |
|---|---|
| 1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
| 2 | Modify src/MainActivity.java file to add Activity code. |
| 3 | Create a new activity with the name of second_main.java and edit it to add activity code. |
| 4 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
| 5 | Modify layout XML file res/layout/second.xml add any GUI component if required. |
| 6 | Modify AndroidManifest.xml to add necessary code. |
| 7 | Run the application and choose a running android device and install the application on it and verify the results. |
Comments
Post a Comment