سؤال

I have a linear layout (layout_container in skeleton.xml) which is inflated with a ListFragment (menufragment) when the activity is created. Then, when the user performs a click, this layout is replaced with another ListFragment (albumsfragment).

The problem is that when I press the back button, I go back indeed to the menufragment but the list is twice longer because it has been filled up again with the same items. How could I avoid this ?

OnCreate method, inside the Activity:

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.skeleton);

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();      
        MenuFragment menufragment = new MenuFragment();  
        fragmentTransaction.add(R.id.layout_container, menufragment, "menufragment");      
        fragmentTransaction.commit();

        }

OnClick method, inside the Activity:

  public void OnMenuClick() {

AlbumsFragment albumsfragment = new AlbumsFragment(); 
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_container, albumsfragment, "albumsfragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

}

My Fragment class code :

public class MenuFragment extends ListFragment{

ArrayList<MenuItem> m_parts = new ArrayList<MenuItem>();

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

            MenuAdapter m_adapter;

            m_parts.add(new MenuItem ("item1"));
            m_parts.add(new MenuItem ("item2"));
            m_parts.add(new MenuItem ("item3"));
            m_parts.add(new MenuItem ("item4"));


            m_adapter = new MenuAdapter(getActivity(), R.layout.menu_row, m_parts);
            setListAdapter(m_adapter);

So each time I go back from AlbumFragment to MenuFragment, the list in MenuFragment grows as item1,item2,item3,item4 are added once again.

I've try to add the check if (savedInstanceState == null) before the transaction of the MenuFragment but doesn't change anything.

What should I do to make MenuFragment be popped up from the back Stack without being inflated again with these items ?

Thanks.

هل كانت مفيدة؟

المحلول

Take a look at the Fragment lifecycle. onActivityCreated() will be called everytime when returning to aFragment. Try, instead, to move your implementation to onCreate() and that should fix your duplication issue.

نصائح أخرى

Probably you can also check for the size of the arraylist and then clear it onActivityCreated() before populating the arrayList.

     if(m_parts != null && m_parts.size()>0){

        //Clear the contents of the arrayList m_parts

          m_parts.clear();
       }

And then add the data to the arrayList.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top