Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems when switching ListView ChoiceMode on screen rotation

Tags:

android

I am trying to build a split pane based on the example code provided at the end of the Android documentation about Fragments, basically a LinearLayout containing two fragments (all the details can be found in the above link):

  • TitlesFragment to display a list of titles
  • DetailsFragment to display the details of the currently selected title

My problem is that the following unexpected behavior happens:

  1. I start the application in Landscape orientation: the titles list is displayed on the left and the details of the currently selected title (the first one at the beginning) are displayed on the right.
  2. I select item X from the titles list: the title becomes highlighted since the ListView is set to CHOICE_MODE_SINGLE and its items implement the Checkable interface.
  3. I switch to Portrait orientation (by rotating the device or emulator): as expected, only the titles list is shown and no item is selected in this configuration, because the ListView gets recreated and, by default, it is set to CHOICE_MODE_NONE.
  4. I select item Y from the titles list: as expected, an activity showing only the DetailsFragment corresponding to the selection made is shown.
  5. I switch back to Landscape orientation (here's where the unexpected behavior happens): the DetailsFragmenton the right correctly shows the details of item Y (which was selected in portrait orientation) but the TitlesFragment still shows item X as selected (i.e. the one which was selected BEFORE the first screen rotation).

Of course, I would like that the last selection made in Portrait orientation was correctly shown also within the TitlesFragment, otherwise I end up with an inconsistent highlighting which shows item X as selected while displaying the details of item Y.

I post the relevant code where the ListView mode is changed (within 'onActivityCreated') and the selected item is set (within the 'showDetails' method):

public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Populate list with our static array of titles.
    setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));

    // Check to see if we have a frame in which to embed the details
    // fragment directly in the containing UI.
    View detailsFrame = getActivity().findViewById(R.id.details);
    mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

    if (savedInstanceState != null) {
        // Restore last state for checked position.
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }

    if (mDualPane) {
        // In dual-pane mode, the list view highlights the selected item.
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        // Make sure our UI is in the correct state.
        showDetails(mCurCheckPosition);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    showDetails(position);
}

/**
 * Helper function to show the details of a selected item, either by
 * displaying a fragment in-place in the current UI, or starting a
 * whole new activity in which it is displayed.
 */
void showDetails(int index) {
    mCurCheckPosition = index;

    if (mDualPane) {
        // We can display everything in-place with fragments, so update
        // the list to highlight the selected item and show the data.
        getListView().setItemChecked(index, true);

        // Check what fragment is currently shown, replace if needed.
        DetailsFragment details = (DetailsFragment)
                getFragmentManager().findFragmentById(R.id.details);
        if (details == null || details.getShownIndex() != index) {
            // Make new fragment to show this selection.
            details = DetailsFragment.newInstance(index);

            // Execute a transaction, replacing any existing fragment
            // with this one inside the frame.
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.details, details);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }

    } else {
        // Otherwise we need to launch a new activity to display
        // the dialog fragment with selected text.
        Intent intent = new Intent();
        intent.setClass(getActivity(), DetailsActivity.class);
        intent.putExtra("index", index);
        startActivity(intent);
    }
}

Does anyone have an idea about how to solve such issue? Thanks in advance.

like image 723
Luke Avatar asked Nov 25 '22 10:11

Luke


1 Answers

Two years later, here I am working with the Fragment Layout sample from Android's v4 support library and I'm stuck with this exact same issue!

Here's the only fix I came up with that doesn't require using CHOICE_MODE_SINGLE all the time:

just add a call to getListView().setItemChecked(mCurCheckPosition, true); in the onResume() method of TitlesFragment. And... that's it!

like image 125
LearningNerd Avatar answered Dec 23 '22 07:12

LearningNerd