android - Fragments Behave Weirdly When Using addToBackStack() -


an sscce issue available on github.

for future readers, original example on branch of same project, , fix available in diff.

this sscce has listview , row of buttons. buttons supposed change data in listview, , listview rows (when clicked) supposed open new fragment , advance backstack while staying in same activity.

if following things, produces following result:

  1. open app.
  2. tap listview. - fragmenttransaction.replace(...) addtobackstack(true)
  3. tap of buttons. - fragmenttransaction.replace(...) addtobackstack(false)
  4. tap button.

result:

overlapping fragments

both fragments become visible, want first loaded fragment (listtwofragment in code) display. how fragments supposed work? if so, how can desired effect?

mainactivity.java:

public class mainactivity extends fragmentactivity implements listtwofragment.callbacks,     listthreefragment.callbacks {     public static final string key_args = "args";      private string cururi = "";     private string curargs = "";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          selectcontent(false);     }      private void selectcontent(boolean addtobackstack) {         fragment fragment;          if (cururi.isempty()) {             // use default fragment             fragment = new listtwofragment();             cururi = listtwofragment.class.getname();         }         else {             try {                 class<fragment> fragmentclass = (class<fragment>) class.forname(cururi);                 fragment = fragmentclass.newinstance();             }             catch (exception e) { // classnotfound, illegalaccess, etc.                 return;             }         }          // configure fragment         bundle args = new bundle();         args.putstring(key_args, curargs);         fragment.setarguments(args);          attachfragment(fragment, addtobackstack, cururi + ";" + curargs, r.id.fragment_container);     }      protected void attachfragment(fragment fragment, boolean addtobackstack, string tag, int replaceid) {         fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();         transaction.replace(replaceid, fragment, tag);         if (addtobackstack) transaction.addtobackstack(tag);         transaction.commit();     }      @override     public void ontwobuttonclick(string title) {         cururi = listtwofragment.class.getname();         curargs = title;         selectcontent(false);     }      @override     public void ontwolistclick() {         cururi = listthreefragment.class.getname();         curargs = "";         selectcontent(true);     }      @override     public void onthreebuttonclick(string title) {         cururi = listthreefragment.class.getname();         curargs = title;         selectcontent(false);     } } 

i'm working fragments to, , way i'm doing it: go forward (add stack), , backwords (remove stack) 2 different functions

to add stack , change fragment:

public void changefragmentaddtostack(fragment mynewfragment) {         fragmenttransaction t = getsupportfragmentmanager().begintransaction();         t.add(r.id.main_fragment, mynewfragment);         t.addtobackstack(null);         t.commit();     } 

to go stack:

        public void gobackstackmain() {     fragmentmanager man = getsupportfragmentmanager();                  if(man.getbackstackentrycount()>0){                      man.popbackstack(man.getbackstackentryat(0).getname(), fragmentmanager.pop_back_stack_inclusive);             } } 

and if want both: go stack , change fragment:

    public void gobackstackandreplacefragment(fragment mynewfragment) { fragmentmanager man = getsupportfragmentmanager();             if(man.getbackstackentrycount()>0){                  int n = man.getbackstackentrycount();                 man.popbackstack(man.getbackstackentryat(n-1).getname(), fragmentmanager.pop_back_stack_inclusive);             }          fragmenttransaction t = getsupportfragmentmanager().begintransaction();         t.replace(r.id.main_fragment, mynewfragment);         t.commit();     } 

i hope !


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -