java - Change fragment by function -
i got fragment based activity in android studio
somefragment fragment = new somefragment(); android.support.v4.app.fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttttransaction.replace(r.id.frame,fragment); fragmenttttransaction.commit();
i invoke function, that:
public void gotofragment(string classname) { ... }
i've tried use class.forname(classname).newinstance() didn't work, android studio shows errors.
thanks in thank in advance
you can this:
private void showfragment(string fragmentid) { fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); fragment fragment = getsupportfragmentmanager().findfragmentbytag(fragmentid); if (fragment == null) { if (fragmentid.equals("type1") { fragment = type1fragment.newinstance(); }else if (fragmentid.equals("type2") { fragment = type2fragment.newinstance(); } ft.replace(r.id.container, fragment, fragmentid).commit(); } }
Comments
Post a Comment