Categories
Android

How to implement Up Navigation

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.

  1. the class MainActivity.java
    package 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 {
    
        @Override
        public 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;
    	}
        }
    
        @Override
        protected 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;

  2. the layout for MainActivity.java
    <?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" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Main activity"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Open child one" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Open child two" />
    
    </LinearLayout>
    
  3. the class ChildOneActivity.java
    package eu.lucazanini.upnavigation;
    
    import android.app.ActionBar;
    import android.app.Activity;
    import android.os.Bundle;
    
    public class ChildOneActivity extends Activity {
    
        @Override
        protected 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

  4. the layout for ChildOneActivity.java
    <?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" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Child one activity"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
    </LinearLayout>
    
  5. the file AndroidManifest.xml
    <?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”

  6. the file ChildTwo.java
    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 implements
    	OnCheckedChangeListener {
    
        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;
    	}
        }
    
        @Override
        protected 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);
        }
    
        @Override
        public 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
  7. the layout for ChildTwoActivity.java
    <?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" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Child two activity"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <RadioGroup
            android:id="@+id/upActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RadioButton
                android:id="@+id/upActivity_Main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Main Activity" />
    
            <RadioButton
                android: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:

    • MainActivity.java
      main_activity
    • ChildOneActivity.java
      childone_activity
  • ChildTwoActivity.java
    childtwo_activity

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.