3

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();
   
 }
 
 
 
 
 
 
 
 
   
}
Sammy
  • 181
  • 3
  • 18

3 Answers3

0

as the Android guide on starting fragments says You don't use a fragment element in your layout file when you want the fragment to change. you should use some layout such as FrameLayout

This is necessary if you plan to change fragments during the life of the activity.

I think this is what is causing your problems.


Your new XML layout is close, but I meant this INSTEAD of using the <fragment> at all.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>  

Then there is still more to do in the code itself, because you will need to create/attach the 'home' fragment when the UI is first created.

inside your public void onCreate(Bundle savedInstanceState) {

add this to create/attach the first 'home' fragment.

 if (findViewById(R.id.fragment_place) != null) {

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create a new Fragment to be placed in the activity layout
        HomeFrag firstFragment = new HomeFrag();

        // In case this activity was started with special instructions from an
        // Intent, pass the Intent's extras to the fragment as arguments
        //   firstFragment.setArguments(getIntent().getExtras());
        // I commented this out since you don't appear to need it right now.

        // Add the fragment to the 'fragment_container' FrameLayout
        getFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
mawalker
  • 2,072
  • 2
  • 22
  • 34
  • http://stackoverflow.com/a/17495250/2801237 here is a good explanation as to why you should use framelayout. – mawalker Dec 30 '15 at 11:29
  • I added FrameLayout and inserted fragment in it but prob is still there. I've edited the layout code in my post. @mawalker – Sammy Dec 30 '15 at 11:32
  • Thanks so much. It works absolutely fine. @mawalker. – Sammy Dec 30 '15 at 11:58
0

Wrap with framelayout. Try this xml

Explaination here

<?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">
    <fragment
        android:id="@+id/fragment_place"
        android:name="com.javacodegeeks.android.fragmentstest.HomeFrag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/L1" />
  </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>

Community
  • 1
  • 1
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
-2

Try This code snippet that will work

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        Fragment currentFragment =   getSupportFragmentManager().findFragmentById(R.id.fragment_place);
        if(currentFragment!=null)
            fragmentTransaction.remove()
        else{
            fragmentTransaction.add(R.id.fragment_place, fr);
            `enter code here`
            fragmentTransaction.commit();
        }
mawalker
  • 2,072
  • 2
  • 22
  • 34