Android Fragment , view is not removed

2019-01-15 08:12发布

I have developed an app, in that I have a fragment transaction,

MainActivity contains Fragment View.

MainActivity have three Button on top of the screen, that remain same when we go this Activity to another Fragment, only MainActivity Fragment part change when we click out of three.

But, my problem is that when I move from the this MainActivity to Fragment when click on First-Button that is OK, but when I click on Second-Button, result is overwrite the screen with first Fragment to another Fragment.

it is in same Activity so , I can not remove Fragment through remove(fr).commit();

Because if I do that then it become non-clickable may be fragment remove so not response on click of next button.

overall result, It display both FirstFragment and NewFragment Screen , when i move to NewFragment how i remove the FirstFragment screen ?

In MainActivity three button have following code to change fragment :

Main Activity :

 public class MasterActivity extends Activity {                
   ImageView imgOne, imgTwo, imgThree;
   Fragment fr;                                              

   @Override                                                 
   protected void onCreate(Bundle savedInstanceState) {      
    super.onCreate(savedInstanceState);                   

    setContentView(R.layout.activity_master);             

    imgOne = (ImageView) findViewById(R.id.imgOne);       
    imgTwo = (ImageView) findViewById(R.id.imgTwo);       
    imgThree = (ImageView) findViewById(R.id.imgThree);


 imgOne.setOnClickListener(new View.OnClickListener() {                                                

       @Override                                                                                         
      public void onClick(View v) {                                                                     
     // TODO Auto-generated method stub                                                            
     fr = new FirstFragment();                                                                          

     FragmentManager fm = getFragmentManager();                                                    
     FragmentTransaction fragmentTransaction = fm.beginTransaction();                              
     fragmentTransaction.replace(R.id.fragment_place, fr);                                         
     fragmentTransaction.addToBackStack(null);                                                     
     //fragmentTransaction.remove(fr).commit();                                                    
     //getFragmentManager().beginTransaction().remove(fr).commit();                                  
     fragmentTransaction.commit();                                                                 
     }                                                                                                 
  });                                                                                                   

  imgTwo.setOnClickListener(new View.OnClickListener() {                                                

    @Override                                                                                         
    public void onClick(View v) {                                                                     
      // TODO Auto-generated method stub                                                            


     fr = new SecondFragment();                                                                           

     FragmentManager fm = getFragmentManager();                                                    
     FragmentTransaction fragmentTransaction = fm.beginTransaction();                              
     fragmentTransaction.replace(R.id.fragment_place, fr);                                         
     fragmentTransaction.addToBackStack(null);                                                     
     //fragmentTransaction.remove(fr).commit();                                                    
     // getFragmentManager().beginTransaction().remove(fr).commit();                                  
     fragmentTransaction.commit();                                                                 
    }                                                                                                 
 });    

its xml file like following :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#000000"
    android:gravity="bottom"
    android:weightSum="3" >

    <ImageView
        android:id="@+id/imgOne"
        android:layout_width="0dp"
        android:layout_height="27dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/img1" />

    <ImageView
        android:id="@+id/imgTwo"
        android:layout_width="0dp"
        android:layout_gravity="center"
        android:layout_height="27dp"
        android:layout_weight="1"
        android:src="@drawable/img2"/>
    <ImageView
        android:id="@+id/imgThree"
        android:layout_width="0dp"
        android:layout_gravity="center"
        android:layout_height="27dp"
        android:layout_weight="1"
        android:src="@drawable/img3" />
</LinearLayout>

 <fragment
     android:id="@+id/fragment_place"
    android:name="packagename.FirstFragment"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.87"/>       
   <LinearLayout/>
 <LinearLayout/>

First Fragment :

public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View r = inflater.inflate(R.layout.first_fratgment, container, false); 

        return r;
    }
}    

Second Fragment :

public class SecondFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View r = inflater.inflate(R.layout.second_fratgment, container, false); 

        return r;
    }
}    

But when click on one button Fragment button to another it display both first and second Fragment screens.

So how to resolve it and how to remove first view when second click?

I used this

fragmentTransaction.remove(fr).commit();

and this

getFragmentManager().beginTransaction().remove(fr).commit();

but it's not working.

7条回答
可以哭但决不认输i
2楼-- · 2019-01-15 08:39

you can use: getSupportFragmentManager().beginTransaction().remove(fragment).commit(); Hope this will help.

查看更多
登录 后发表回答