Search code examples
androidandroid-listfragmentandroid-dialogfragment

Data in ListFragment if added multiple time every time it re-opened


I have a ListFragment inside a DialogFragment, I am passing list data to ListFragment from Activity->DialogFragment->ListFragment. Issue is every time I open the dialog from same instance of Activity List is shown with list data getting appended to previously drawn list. Here is the snapshot of the code I am working with.

class TestActivity extends FragmentActivity {

public void onButtonClick() {
    TestDialogFragment.newInstance(data).show(getSupportFragmentManager(), null);
    }
}

class TestDialogFragment extends DialogFragment {

    Data data;

    public static TestDialogFragment newInstance(Data data) {
        final TestDialogFragment fragment = new TestDialogFragment();
        // add input to fragment argument
        return fragment;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View dialogView = inflater.inflate(R.layout.fragment_test_dialog, container, false);
        // read data from fragment argument
        return dialogView;
    }

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

        final Fragment fragment = TestListFragment.newInstance(testList);
        final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.chapter_list, fragment).commit();
    }    

    static ArrayList<String> testList = new ArrayList<>();
    {
        testList.add("Test 1");
        testList.add("Test 2");
    }

}

class TestListFragment extends ListFragment {
    public static ChapterListFragment newInstance(final ArrayList<String> testList) {
        final TestListFragment fragment = new TestListFragment();
        // add input to fragment argument
        return fragment;
    }

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // read data from fragment argument
        setListAdapter(new TestListAdapter(testList));
    }   


    class ChapterListAdapter extends ArrayAdapter<String> {

        public ChapterListAdapter(final ArrayList<String> testList) {
            super(getActivity(), R.layout.view_test_list_item, testList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        ...
        }

    }    
}

Solution

  • This is where your problem is:

    static ArrayList<String> testList = new ArrayList<>();
    {
        testList.add("Test 1");
        testList.add("Test 2");
    }
    

    Your testList is static, thus initialized once, when the class is loaded. You add your items in a non-static initializer block, and those are executed every time you instantiate a new instance of your class.

    Maybe a newline clarifies this problem a bit:

    static ArrayList<String> testList = new ArrayList<>();
    
    {
            testList.add("Test 1");
            testList.add("Test 2");
    }
    

    If you make your initializer block static, then the items will only be added once.

    static ArrayList<String> testList = new ArrayList<>();
    static {
        testList.add("Test 1");
        testList.add("Test 2");
    }