An Android ListView with the Back/Up/Home button enabled

Without much discussion, here’s an Android ListView/ListFragment with its Back/Up/Home button enabled:

An Android ListFragment/ListView with Back/Up button enabled

(That button used to be a Home button, but now it’s used for the Back/Up action.)

And here’s the source code for that ListView/ListFragment:

package com.valleyprogramming.playcaller;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.valleyprogramming.playcaller.dao.DatabaseHelper;
import com.valleyprogramming.playcaller.dao.DatabaseManager;

import java.util.ArrayList;

public class ListTeamsFragment extends ListFragment {

    public static final String TEAM_NAME_INTENT_KEY = "TEAM_NAME";

    public void onCreate(Bundle savedInstanceState) {

        DatabaseHelper.TeamsCursor tc = DatabaseManager.get(getActivity()).getAllTeams();
        ArrayList<String> listOfTeamNames = new ArrayList<>();
        for(tc.moveToFirst(); !tc.isAfterLast(); tc.moveToNext()) {

        // enables the "back/up" icon (left arrow). nothing more than that.

        // part of the process of enabling the options menu, which includes the
        // back/up button. see onOptionsItemSelected(menu) for handling.

        // list the team names with an adapter that talks to our listview
        TeamNamesAdapter adapter = new TeamNamesAdapter(listOfTeamNames);

     * called when a user taps on 'options menu' menu item. essentially a
     * handler for that process. only used here to let the user go back to
     * the previous screen.
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
                return true;
                return super.onOptionsItemSelected(item);

     * when a team name is tapped, show the "configure player ratings" screen
     * for that team.
     * TODO: in the future this should go to a "view player ratings" screen, but for now
     * go directly to the editor.
    public void onListItemClick(ListView l, View v, int position, long id) {
        String teamName = ((TeamNamesAdapter)getListAdapter()).getItem(position);

        // pass the select team name to the player ratings activity as you launch it
        Intent intent = new Intent(getActivity(), ConfigurePlayerRatingsActivity.class);
        intent.putExtra(TEAM_NAME_INTENT_KEY, teamName);

    private class TeamNamesAdapter extends ArrayAdapter<String> {

        public TeamNamesAdapter(ArrayList<String> teams) {



For the purposes of the Back/Up/Home button, the important parts of that code are:

  • getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
  • setHasOptionsMenu(true);
  • the code in the onOptionsItemSelected method

If you need to see how to enable the Back/Up button in an Android ListView/ListFragment, I hope this is helpful.