New Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/L1" >
<fragment
android:id="@+id/fragment_place"
android:name="com.javacodegeeks.android.fragmentstest.HomeFrag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<RelativeLayout
android:id="@+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<ImageView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home"
android:onClick="selectFrag"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search"
android:onClick="selectFrag"
android:layout_toRightOf="@+id/button1"/>
<ImageView
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/map"
android:onClick="selectFrag"
android:layout_toRightOf="@+id/button2"/>
</RelativeLayout>
</RelativeLayout>
I am learning to change fragments on ImageView click using FragmentManager. When i click the 1st imageView, the main fragment layout with text "home" shows good. But when the click on other ImageViews to change fragment page, it changes to new fragment page but without removing the previous page. Result, previous page gets mashed up with new page. It look like this. The screenshot shows home page with text as "home" which is the previous page and the new page with text "search". How to remove the previous page when new ImageView is clicked?
MainActivity
public class MainActivity extends Activity {
private static final int RESULT_SETTINGS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
}
public void selectFrag(View view) {
Fragment fr = null;
if(view == findViewById(R.id.button1)) {
fr = new HomeFrag();
}else if(view == findViewById(R.id.button2)){
fr = new SearchFrag();
}else if(view == findViewById(R.id.button3)){
fr = new MapFrag();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}