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


i have listfragment inside dialogfragment, passing list data listfragment activity->dialogfragment->listfragment. issue every time open dialog same instance of activity list shown list data getting appended drawn list. here snapshot of code 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 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 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 fragment argument         return fragment;     }      @override     public void onactivitycreated(final bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         // read data 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) {         ...         }      }     } 

this problem is:

static arraylist<string> testlist = new arraylist<>(); {     testlist.add("test 1");     testlist.add("test 2"); } 

your testlist static, initialized once, when class loaded. add items in non-static initializer block, , executed every time instantiate new instance of class.

maybe newline clarifies problem bit:

static arraylist<string> testlist = new arraylist<>();  {         testlist.add("test 1");         testlist.add("test 2"); } 

if make initializer block static, items added once.

static arraylist<string> testlist = new arraylist<>(); static {     testlist.add("test 1");     testlist.add("test 2"); } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -