By Alvin Alexander. Last updated: August 29, 2017
Without much discussion, here’s an Android ListView
/ListFragment
with its Back/Up/Home 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.app.ListFragment; 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"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActivity().setTitle(R.string.teams_list_title); DatabaseHelper.TeamsCursor tc = DatabaseManager.get(getActivity()).getAllTeams(); ArrayList<String> listOfTeamNames = new ArrayList<>(); for(tc.moveToFirst(); !tc.isAfterLast(); tc.moveToNext()) { listOfTeamNames.add(tc.getTeam().teamName); } // enables the "back/up" icon (left arrow). nothing more than that. getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); // part of the process of enabling the options menu, which includes the // back/up button. see onOptionsItemSelected(menu) for handling. setHasOptionsMenu(true); // list the team names with an adapter that talks to our listview TeamNamesAdapter adapter = new TeamNamesAdapter(listOfTeamNames); setListAdapter(adapter); } /** * 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. */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: getActivity().onBackPressed(); return true; default: 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); startActivity(intent); } private class TeamNamesAdapter extends ArrayAdapter<String> { public TeamNamesAdapter(ArrayList<String> teams) { super( getActivity(), android.R.layout.simple_list_item_1, 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.