android - Replace Fragment and remove old Fragment -


this main xml

<?xml version="1.0" encoding="utf-8"?><framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_place" android:layout_width="wrap_content" android:layout_height="fill_parent"/> 

this main activity

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     fragmentmanager fm = getfragmentmanager();     fragmenttransaction fragmenttransaction = fm.begintransaction();     fragmenttransaction.add(r.id.fragment_place, new thirdclass());     fragmenttransaction.commit(); } 

this thirdclass

public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.test_fragment1, container, false);     fragmentmanager fm = getfragmentmanager();     fragmenttransaction fragmenttransaction = fm.begintransaction();     fragmenttransaction.add(r.id.fragment_place, new fourthclass());     fragmenttransaction.commit();     return v; } 

this layout of thirdclass

<?xml version="1.0" encoding="utf-8"?><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second fragment"/></linearlayout> 

this fourthclass

public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.test_frag, container, false);     return v; } 

and layout

<?xml version="1.0" encoding="utf-8"?><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><textview android:layout_width=" android:layout_height=" android:text="third fragment"/></linearlayout> 

my question fragments being saved not replacing each other , shows me this.enter image description here

use fragmenttransaction.replace() instead of fragmenttransaction.add()

from the documentation:

replace existing fragment added container. same calling remove(fragment) added fragments added same containerviewid , add(int, fragment, string) same arguments given here.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -