Android compare two fragments -


when change fragment compare new 1 current one. if same, not necessary replace them.

i tried

 public void displayfragment(fragment fragment){     fragmentmanager fm = getsupportfragmentmanager();     fragment currentfragment = fm.findfragmentbyid(r.id.content_frame);     if(!currentfragment.equals(fragment))         getsupportfragmentmanager().begintransaction().addtobackstack(null).replace(r.id.content_frame, fragment).commit(); } 

but not work , fragment changed if new 1 same

a solution ? thanks

edit

the solution :

public void displayfragment(fragment fragment){     fragmentmanager fm = getsupportfragmentmanager();     fragment currentfragment = fm.findfragmentbyid(r.id.content_frame);     if(!fragment.getclass().tostring().equals(currentfragment.gettag())){         getsupportfragmentmanager()         .begintransaction()         .addtobackstack(null)         .replace(r.id.content_frame, fragment, fragment.getclass().tostring()) // add , tag new fragment         .commit();     } } 

one way identify fragments give them tags :

public void displayfragment(fragment fragment, string tag){     fragmentmanager fm = getsupportfragmentmanager();     fragment currentfragment = fm.findfragmentbyid(r.id.content_frame);     if(!tag.equals(currentfragment.gettag())         getsupportfragmentmanager()         .begintransaction()         .addtobackstack(null)         .replace(r.id.content_frame, fragment, tag) // add , tag new fragment         .commit(); } 

edit : alternative version psv's comment, using classnames tag :

public void displayfragment(fragment fragment){      fragmentmanager fm = getsupportfragmentmanager();     fragment currentfragment = fm.findfragmentbyid(r.id.content_frame);     if(!fragment.getclass().tostring().equals(currentfragment.gettag()))     {          getsupportfragmentmanager()         .begintransaction()         .addtobackstack(null)         .replace(r.id.content_frame, fragment, fragment.getclass().tostring()) // add , tag new fragment         .commit();      } }  

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -