android - Navigation drawer fragments overlap even with replace() -
i have navigation drawer in activity. activity's layout has relative layout fragment. id of relativelayout maincontent
in load fragment(call fraga) in oncreate()
of activity. have same fragment in navigation drawer along other navigation items. loaded oncreate of activity , can loaded navigation drawer.
on selecting navigation items, replace fragment in maincontent
relativelayout in activity replace()
. in navigation drawer, 0th item "fragb"(the same fraga in nav drawer). second fragment(fragc). when select fragb "more once" , select fraga , press button, fragb , fragc overlap.
and have keep pressing button go initial screen because every time select item nav drawer, new fragments fragbs , fragcs created. not replaced. replace them in maincontent
(first relative layout) in activity's layout. i'm using framelayout
fragments.
this activity's layout in place fragments:
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.prematixsofs.taxiapp.datevehiclepicker"> <!-- main content view --> <relativelayout android:id="@+id/maincontent" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_gravity="center_horizontal|bottom"></include> </relativelayout> <!-- navigation drawer --> <relativelayout android:id="@+id/drawerpane" android:layout_width="230dp" android:layout_height="match_parent" android:layout_gravity="start"> <!-- profile box --> <relativelayout android:id="@+id/profilebox" android:layout_width="match_parent" android:layout_height="80dp" android:background="#ff02c7c4" android:padding="8dp"> <imageview android:id="@+id/avatar" android:layout_width="50dp" android:layout_height="50dp" android:layout_margintop="15dp" android:src="@drawable/user" /> <linearlayout android:layout_width="wrap_content" android:layout_height="42dp" android:layout_centervertical="true" android:layout_marginleft="15dp" android:layout_torightof="@+id/avatar" android:orientation="vertical"> <textview android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:textcolor="#fff" android:textsize="16sp" android:textstyle="bold" /> <textview android:id="@+id/viewprofile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_margintop="4dp" android:text="view profile" android:textcolor="#fff" android:textsize="12sp" /> </linearlayout> </relativelayout> <!-- list of actions (pages) --> <listview android:id="@+id/navlist" android:layout_width="280dp" android:layout_height="match_parent" android:layout_below="@+id/profilebox" android:background="#ffffffff" android:choicemode="singlechoice" /> <textview android:id="@+id/invisibletextviewid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> </relativelayout>
this activity's code load fraga:
android.support.v4.app.fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.add(r.id.maincontent, new datetimepicker(),"tags").commit();
this nav drawer onselect:
private void selectitemfromdrawer(int position) { fragment fragment = null; switch (position){ case 0: fragment = new datetimepicker(); break; case 1: fragment = new preferencesfragment(); break; case 2: mdrawerlayout.closedrawer(mdrawerpane); alertdialog.builder alertdialog = new alertdialog.builder(datevehiclepicker.this); alertdialog.settitle("logout"); // setting dialog message alertdialog.setmessage("do want logout?"); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { sessionmanager.logoutuser(); } }); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent intent = new intent(getapplicationcontext(), mainactivity.class); intent.setflags(intent.flag_activity_clear_top); intent.putextra("exit", true); startactivity(intent); dialog.cancel(); } }); alertdialog.show(); } if(fragment!=null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.addtobackstack(null).replace(r.id.maincontent, fragment); fragmenttransaction.commit(); mdrawerlayout.closedrawer(mdrawerpane); }
this set listener nav drawer:
mdrawerlist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitemfromdrawer(position); } });
i call method selectitemfromdrawer(position);
you must handle backstack must clear fragment on stack when added or replace main fragment
@override public void onbackpressed() { logg.e(tag, getfragmentmanager().getbackstackentrycount()); try { if (mnavigationdrawerfragment.isdraweropen()) { mnavigationdrawerfragment.closedrawer(); } if (getfragmentmanager().getbackstackentrycount() > 1) { getfragmentmanager().popbackstack(); } else { exit(); } catch (exception e) { e.printstacktrace(); } }
Comments
Post a Comment