Android Fragment contributes to the Action Bar when not on the screen -
if dynamically add fragment
(using fragmentmanager
) container defined in landscape xml switch portrait, dynamically injected fragment
still exists. contributing action bar
though not visible. way / design prevent happening?
i have tried using isvisible
in oncreateoptionsmenu
of fragment
causes issues on android versions because oncreateoptionsmenu
called before oncreateview
results in false
if fragment going visible current configuration.
note: not handling configuration myself. haven't specified configchanges
in manifest , not overriding onconfigurationchanged
.
activity:
// inject detail fragment fragment detailfragment = getsupportfragmentmanager().findfragmentbyid(r.id.detail_container); if(detailfragment == null) getsupportfragmentmanager().begintransaction().replace(r.id.detail_container, detailfragment.newinstance(id)).commit(); // inject master fragment if(findviewbyid(r.id.master_container) != null) { masterdetail = true; fragment listfragment = getsupportfragmentmanager().findfragmentbyid(r.id.master_container); if(listfragment == null) getsupportfragmentmanager().begintransaction().replace(r.id.master_container, listfragment.newinstance(position)).commit(); }
activity portrait xml
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/detail_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
activity landscape xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselinealigned="false"> <framelayout android:id="@+id/master_container" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <framelayout android:id="@+id/detail_container" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2"/> </linearlayout>
the model i'd follow based on explanation.
public class myactivity extends activity { boolean mmultipane = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); framelayout mastercontainer = (framelayout) findviewbyid(r.id.master_container); if (mastercontainer != null) mmultipane = true; ... } }
from on in activity
can use mmultipane
variable change behaviour including changing how options menu set (just have 2 different menu.xml
files or add / remove menu items depending on mode you're in).
Comments
Post a Comment