home / blog

Android dev: app navigation using back button

The back button can be used to navigate within an application. The easiest way to do this is to define multiple activities. Activities can be moved into using action buttons etc, and then can be exited using the back button. This should work to any number of levels.

Activities are defined in AndroidManifest.xml. Below is a definition of a “main” activity (com.adamish.android.dmxapp.activity.Main) and a “sub-activity” (com.adamish.android.dmxapp.activity.Settings). The only difference between the activities is that the main activity has a launcher category associated with it, this means that it appears on the application.

<manifest 
 xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.adamish.android.dmxapp" 
 android:versionCode="1"
 android:versionName="1.0">

 <application
   android:name="DmxApp"
   android:icon="@drawable/icon"

  <-- main activity -->
  <activity
    android:name=".activity.Main"
    android:label="@string/app_name">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>

  <-- sub-activity -->
  <activity 
    android:name=".activity.Settings"
    android:label="@string/app_name">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
    </intent-filter>
  </activity>

Below is an example of code used to change the activity, in this case a button on the main screen that enters the settings activity.

Button settings = (Button)findViewById(R.id.settings);
settings.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    startActivity(new Intent(Main.this, Settings.class));
  }
});
This entry was posted in geek and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.