java - How to "pre-load" a fragments view tree? -
i have app uses fragments display view (wether or not shouldn´t discussed here please.)
i have fragment ifirstfragment
modally shows isecondfragment
. view tree (= xml file) of isecondfragment
rather complex (many views). on slow devices notice significant loading time (~1 second) when displaying isecondfragment
.
i want reduce "loading time" instant using code. call hack, trick, or common sense:
public class mainactivity extends appcompatactivity { firstfragment ifirstfragment; secondfragment isecondfragment; fragmenttransaction ifragmenttransaction; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ifirstfragment = new firstfragment(); isecondfragment = new secondfragment(); // adding isecondfragment , instantly hide it, view inflated. ifragmenttransaction= getsupportfragmentmanager().begintransaction(); ifragmenttransaction.add(r.id.fragment_container, ifirstfragment, firstfragment.fragment_tag); ifragmenttransaction.add(r.id.fragment_container, isecondfragment, secondfragment.fragment_tag); ifragmenttransaction.hide(isecondfragment); ifragmenttransaction.commit(); } @override protected void onstart() { super.onstart(); } public void showsecondfragment() { ifragmenttransaction= getsupportfragmentmanager().begintransaction(); ifragmenttransaction.hide(ifirstfragment); ifragmenttransaction.show(isecondfragment); ifragmenttransaction.addtobackstack(null); ifragmenttransaction.commit(); }
}
when mainactivity
loaded, both add firstfragment
, secondfragment
activity, instantly hide secondfragment
. firstfragment
shown now. now, when button pressed , showsecondfragment()
called, instead of doing fragmenttransaction.replace()
show
fragment. i hoping show the secondfragment
instantly, still loads longer.
i created minimal working example problem @ github: fragmenttransactiontest
Comments
Post a Comment