android - Replace one fragment with an another fragment -


i want replace old fragment new fragment, still buttons of old fragment still visible in new fragment.

in old one, on button click enter image description here

fragmenttransaction transaction = getfragmentmanager().begintransaction(); fragment newfragment = genericmood.newinstance("a","b");  // replace whatever in fragment_container view fragment, // , add transaction stack if needed transaction.replace(r.id.allmoods, newfragment); transaction.addtobackstack(null); transaction.commitallowingstateloss(); 

i can replace old fragment new one, buttons r.id.allmoods fragment still visible on top of new fragment.

enter image description here

i tried given below code.

fragmenttransaction transaction = getfragmentmanager().begintransaction(); fragment newfragment = genericmood.newinstance("a","b");  // replace whatever in fragment_container view fragment, // , add transaction stack if needed transaction.replace(((viewgroup)getview().getparent()).getid(), newfragment); transaction.addtobackstack(null); transaction.commitallowingstateloss(); 

xml files:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/allmoods"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@color/colorprimary"     tools:context="com.moodoff.moods">      <button         android:text="button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margintop="64dp"         android:id="@+id/btn_btn"         android:height="80dp"         android:width="100dp"         android:onclick="putmeoff"         android:layout_marginleft="17dp"         android:layout_marginstart="17dp"/> </relativelayout> 

this fragment supposed replace above:

<framelayout 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:id="@+id/genericmood"     android:layout_height="match_parent"     android:background="@color/colorprimary"     tools:context="com.moodoff.genericmood">     <!-- todo: update blank fragment layout -->       <relativelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:background="#000000"         android:layout_gravity="fill_horizontal"         android:id="@+id/floatingbuttons"         >         <android.support.design.widget.floatingactionbutton             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_margintop="1dp"             android:layout_marginright="14dp"             app:backgroundtint="#ffffff"             android:layout_alignparenttop="true"             android:layout_alignparentright="true"             android:layout_alignparentend="true"             android:src="@drawable/cameraicon"             android:id="@+id/btn_camera"             app:fabsize="mini"             />     </relativelayout> </framelayout> 

both doesn't work. do? update: after replacing proper container buttons had gone new fragment not getting instantiated properly. gets pure blank white screen. enter image description here

my activity_alltabs.xml looks this:   <?xml version="1.0" encoding="utf-8"?>     <android.support.design.widget.coordinatorlayout 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:id="@+id/main_content"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:fitssystemwindows="true"         tools:context="com.moodoff.alltabs">          <android.support.design.widget.appbarlayout             android:id="@+id/appbar"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:theme="@style/apptheme.appbaroverlay">              <android.support.design.widget.tablayout                 android:id="@+id/tabs"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:background="@android:color/background_dark" />          </android.support.design.widget.appbarlayout>          <android.support.v4.view.viewpager             android:id="@+id/container"             android:layout_width="match_parent"             android:layout_height="match_parent"             app:layout_behavior="@string/appbar_scrolling_view_behavior" />      </android.support.design.widget.coordinatorlayout> 

i have worked on fragments before , hope out , give better understanding of flow. firstly, mainactivity.xml file :

<?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"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.example.activity.homeactivity">       //this framelayout contain fragments view.    <framelayout       android:id="@+id/container_view"       android:layout_width="match_parent"       android:layout_height="match_parent">     </framelayout>   </relativelayout> 

next, create 2 fragments , xml mentioned below : fragment1.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:clickable="true" // important have     tools:context=".fragments.frament1">       <button         android:id="@+id/btn"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_gravity="bottom"/>   </relativelayout> 

the next fragment same mentioned above. here fragment1.class:

public class fragment1 extends fragment implements view.onclicklistener { button btn;     public fragment1() {     // required empty public constructor }       @override      public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {         view view = inflater.inflate(r.layout.fragment1, container, false);         view.setbackgroundcolor(color.white);         //perform required conditions , return view         button = (button) view.findviewbyid(r.id.btn);        button.setonclicklistener(this);         return view;       }          public void onclick(view v) {               switch(v.getid())              {                 case r.id.btn:                   //replace current fragment on button click                   fragment fragment2= new fragment2();                   getfragmentmanager().begintransaction().                  replace(r.id.container_view, fragment2).                  addtobackstack("frags").commit();                 break;              }         }  } 

and fragment2 follows :

 public class fragment2 extends fragment{  string tag = "fragment2";        public fragment2() {       // required empty public constructor    }       @override      public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {            view view = inflater.inflate(r.layout.fragment2,container,false);           view.setbackgroundcolor(color.white);        return view;     } } 

as mentioned earlier, xml file same fragment1.xml. more important here main activity contain layout take fragments view when ever user switches fragments. hence use replace method replace previous view fragments view specified us.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -