android - Avoid Activity layout showing before Fragment is created -
i have mainactivity
starts detailsactivity
. detailsactivity
layout contains 1 framelayout
needed displaying detailsfragment
.
so, when user clicks button mainactivity
, detailsactivity
started:
public class detailsactivity extends actionbaractivity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_info); displayfragment(); } public void displayfragment(){ fragment fragment = detailsfragment.newinstance(); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.replace(r.id.fragment_container, fragment); transaction.commit(); } }
the problem detailsactivity
oncreate
finishes before detailsfragment
oncreateview
, user sees blank activity layout multiple milliseconds. there way avoid it?
this not possible.
in order show fragment
must put container. in case container (r.id.fragment_container) part of main layout (r.layout.activity_info).
consequentially, in order r.id.fragment_container available accept fragment
, r.layout.activity_info must have been inflated prior calling following...
transaction.replace(r.id.fragment_container, fragment); transaction.commit();
unfortunately act of commiting fragmenttransaction
asynchronous and, importantly, you've had call...
setcontentview(r.layout.activity_info);
...before commiting transaction in order have valid reference framelayout
(via r.id.fragment_container).
the time takes fragment
become visible vary based on number of things (device capabilities, complexity of fragment
layout , ancillary code etc). may possible fiddle process order there no official way of doing , results more or less successful on different android devices.
if you're worried users seeing blank screen short time i'd recommend have activity
create progressdialog
simple "please wait..." message - create dialog before commiting transaction , dismiss once fragment
has been created.
Comments
Post a Comment