Leave the selected position of the list fragment when changing orientation

I have a list fragment to the left of another fragment and is essentially a standard click of an element and the correct fragment of the fragment is updated. When they click on an item in the list fragment, they select the category of news articles, and I need to save everything that is selected when they rotate the device. How can I do it? My current code is not working.

My code is as follows:

public class SideMenuFragment extends ListFragment {
    ArrayList<SideItem> sideItems;
    SideAdapter sideAdapter;
    public SideMenuFragment() {
        this.setRetainInstance(true);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sideItems = new ArrayList<SideItem>();
                ...add bunch of items
        sideAdapter = new SideAdapter(getActivity(), sideItems);
        getListView().setVerticalScrollBarEnabled(false);
        setListAdapter(sideAdapter);
        if (savedInstanceState != null) {
            sideAdapter.setSelectedItem(savedInstanceState.getInt("sidePosition"));
            sideAdapter.notifyDataSetChanged();
                }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("sidePosition", sideAdapter.getSelectedItem());
    }

    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
        if (sideAdapter.getSelectedItem() != position) {
            sideAdapter.setSelectedItem(position);
            sideAdapter.notifyDataSetChanged();
        }
        switch (position) {
                      ...switch the fragment depending on position.
                    }
    }

    // the meat of switching the above fragment
    private void switchFragment(Fragment fragment, String title) {
        if (getActivity() == null)
            return;
        if (getActivity() instanceof HomeActivity) {
            HomeActivity a = (HomeActivity) getActivity();
            a.switchContent(fragment, title);
        }
    }
}
+2
source share
1 answer

First, add your snippet in xml if the layout is activity. In Activity onCreate

getFragmentManager().findFragmentById(R.id.youtfragmentid).setRetainInstance(true)

This means that the fragment will not be recreated when activity is restored.

ListView onActivityCreated - . - .

, . , onActivityCreated,

if (sideAdapter == null) {

    sideAdapter = new SideAdapter(getActivity(), sideItems);
        getListView().setVerticalScrollBarEnabled(false);
        setListAdapter(sideAdapter);

}

, onCreateView .

private View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (v == null) {
        v = inflater.inflate(R.layout.list, null);
    } else {
        // detatch from container and return the same view
        ((ViewGroup) getListView().getParent()).removeAllViews();
    }
    return v;
}
0

Source: https://habr.com/ru/post/1609462/


All Articles