In this example you can see how to implement the Up Navigation, i.e. the option to go back to a previous activity using the button in the upper left of the action bar.
The previous activity can be a fixed activity or an activity determined at runtime.
The following steps are the most important files, but you can download the full example here.
- the class MainActivity.java
1234567891011121314151617181920212223242526272829303132333435363738394041package eu.lucazanini.upnavigation;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {@Overridepublic void onClick(View v) {Intent intent;switch (v.getId()) {case R.id.button1:intent = new Intent(getApplicationContext(), ChildOneActivity.class);startActivity(intent);break;case R.id.button2:intent = new Intent(getApplicationContext(), ChildTwoActivity.class);startActivity(intent);break;default:break;}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);Button buttonOne = (Button) findViewById(R.id.button1);buttonOne.setOnClickListener(this);Button buttonTwo = (Button) findViewById(R.id.button2);buttonTwo.setOnClickListener(this);}}
this activity is the main one, it implements the layout defined in /res/layout/main_activity.xml which consists of two buttons that launch two other activities where there is the Up Navigation button; - the layout for MainActivity.java
1234567891011121314151617181920212223242526<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Main activity"android:textAppearance="?android:attr/textAppearanceLarge" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Open child one" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Open child two" /></LinearLayout> - the class ChildOneActivity.java
123456789101112131415161718]package eu.lucazanini.upnavigation;import android.app.ActionBar;import android.app.Activity;import android.os.Bundle;public class ChildOneActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.child_one_activity);ActionBar actionBar = getActionBar();actionBar.setDisplayHomeAsUpEnabled(true);}}
this class implements Up Navigation at the line 16, and the activity that is launched by pressing the “Up Navigation” button is defined in AndroidManifest.xml - the layout for ChildOneActivity.java
1234567891011121314<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Child one activity"android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout> - the file AndroidManifest.xml
12345678910111213141516171819202122232425262728293031323334]<?xml version="1.0" encoding="UTF-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="eu.lucazanini.upnavigation" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /><application android:allowBackup="true" android:icon="@drawable/ic_launcher"android:label="@string/app_name" android:theme="@style/AppTheme"><activity android:name="MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="ChildOneActivity"android:parentActivityName="eu.lucazanini.upnavigation.MainActivity"><!-- Parent activity meta-data to support API level 7+ --><meta-data android:name="android.support.PARENT_ACTIVITY"android:value="eu.lucazanini.upnavigation.MainActivity" /></activity><activity android:name="ChildTwoActivity"android:parentActivityName="eu.lucazanini.upnavigation.MainActivity"><!-- Parent activity meta-data to support API level 7+ --><meta-data android:name="android.support.PARENT_ACTIVITY"android:value="eu.lucazanini.upnavigation.MainActivity" /></activity></application></manifest>
at the lines 19-30 I set the parent activity for the two activities “Child” - the file ChildTwo.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859]package eu.lucazanini.upnavigation;import android.app.ActionBar;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NavUtils;import android.view.MenuItem;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class ChildTwoActivity extends Activity implementsOnCheckedChangeListener {private int parentActivityId;public void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.upActivity_Main:parentActivityId = R.id.upActivity_Main;break;case R.id.upActivity_ChildOne:parentActivityId = R.id.upActivity_ChildOne;break;}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.child_two_activity);parentActivityId = R.id.upActivity_Main;RadioGroup rg = (RadioGroup) findViewById(R.id.upActivity);rg.setOnCheckedChangeListener(this);ActionBar actionBar = getActionBar();actionBar.setDisplayHomeAsUpEnabled(true);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case android.R.id.home:if (parentActivityId == R.id.upActivity_ChildOne) {Intent intent = new Intent(this, ChildOneActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);startActivity(intent);} else {NavUtils.navigateUpFromSameTask(this);}return true;}return super.onOptionsItemSelected(item);}}
as in ChildOneActivity.java also here there is the line 40 to enable the Up Navigation, but in this case the user can choose whether to launch the main activity defined in AndroidManifest.xml or to launch the activity ChildOne using the two radio buttons- lines 18-25: in the field parentActivityId I save the user’s choice
- lines 46-53: if the user presses the button “Up Navigation” (line 47: case android.R.id.home) and if the user has selected “Child One” it launches the activity ChildOneActivity else it launches the activity set in AndroidManifest.xml
- the layout for ChildTwoActivity.java
123456789101112131415161718192021222324252627282930313233<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Child two activity"android:textAppearance="?android:attr/textAppearanceLarge" /><RadioGroupandroid:id="@+id/upActivity"android:layout_width="wrap_content"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/upActivity_Main"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="Main Activity" /><RadioButtonandroid:id="@+id/upActivity_ChildOne"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Child One" /></RadioGroup></LinearLayout>
The three activities of this example: