android - How to remove Fragment from an Activity? -


i'm trying remove fragment mainactivity after clicking choice in slide menu. cannot find how implement it.

i looked online , found fragmenttransaction class think have wrong implementation.

mainactivity java;

 @override     public boolean onnavigationitemselected(menuitem item) {         // handle navigation view item clicks here.         int id = item.getitemid();          if (id == r.id.technology) {             fragmenttransaction.remove(r.layout.frag);           } else if (id == r.id.opinion) {           } else if (id == r.id.travel) {          } else if (id == r.id.politics) {          }else if(id == r.id.home){         }          drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         drawer.closedrawer(gravitycompat.start);         return true; 

xml file main_content;

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:background="#692f2f"     app:layout_behavior="@string/appbar_scrolling_view_behavior"     tools:showin="@layout/app_bar_main">       <fragment         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/fragment"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true"         android:layout_margintop="53dp"         tools:layout="@layout/frag" /> </relativelayout> 

sorry english.

if want able manage fragments @ runtime, should not hard code them in xml file instead put empty container in xml this:

<framelayout         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/fragment"         /> 

you not want use wrap_content both layout_width , layout_height make placement easier. margin can go directly on content of fragment rather fragment otherwise, can put in main_activity xml layout file.

and have inflate in fragment class:

@override     public view oncreateview(layoutinflater inflater,              viewgroup container, bundle savedinstancestate) {          return inflater.inflate(r.layout.your_fragment_layout_here, container, null);     } 

then in code, should have fragmentmanager use fragmenttransaction , example:

private fragmentmanager fm; private fragmenttransaction ft 

in oncreate function :

fm = getfragmentmanager(); 

then :

@override     public boolean onnavigationitemselected(menuitem item) {         // handle navigation view item clicks here.         int id = item.getitemid();         ft = fm.begintransaction();          if (id == r.id.technology) {             ft.remove (fragment_instance_to_remove_here);             ft.commit();           } else if (id == r.id.opinion) {           } else if (id == r.id.travel) {          } else if (id == r.id.politics) {          }else if(id == r.id.home){         }          drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         drawer.closedrawer(gravitycompat.start);         return true; } 

for more explanations fragmentmanager .

hope it's help.


Comments