Code example for ListFragment

Methods: getListViewonViewCreatedsetListAdapter, setRetainInstance

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated item position. 
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
        ArrayList<Student> mStudents = mPeriod.getmStudents();
        StudentAdapter adapter = new StudentAdapter(mStudents);
        // TODO: replace with a real list adapter. 
    public void onAttach(Activity activity) {
        // Activities containing this fragment must implement its callbacks. 
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        mCallbacks = (Callbacks) activity;
    public void onDetach() { 
        // Reset the active callbacks interface to the dummy implementation. 
        mCallbacks = null;
    public void updateUI() { 
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
        Student s = ((StudentAdapter)getListAdapter()).getItem(position);
        // Notify the active callbacks interface (the activity, if the 
        // fragment is attached to one) that an item has been selected. 
    public void onSaveInstanceState(Bundle outState) {
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            // Serialize and persist the activated item position. 
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
     * Turns on activate-on-click mode. When this mode is on, list items will be 
     * given the 'activated' state when touched. 
    public void setActivateOnItemClick(boolean activateOnItemClick) {
        // When setting CHOICE_MODE_SINGLE, ListView will automatically 
        // give items the 'activated' state when touched. 
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    private void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else { 
            getListView().setItemChecked(position, true);
        mActivatedPosition = position;
Connect your IDE to all the code out there  Get Codota for Java