Fragments: Remove all fragments in a view

后端 未结 5 762
终归单人心
终归单人心 2020-12-16 17:25

The scenario I am faced with, is in my application I have a single pane and dual pane style layout. Rather than individually handle every single navigation

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 17:56

    Turns out FragmentTransaction.replace() is the correct operation and should work correctly. It only does not work when using ActionBarSherlock and SherlockFragmentActivity so I can only assume it is a bug in this compatibility library.

    I have confirmed this through using the code below to implement the desired behaviour through Android on API11+, the android compatibility library, and ActionBarSherlock. It only breaks in the last instance.

    package com.test.test;
    
    import com.actionbarsherlock.app.SherlockFragmentActivity;
    
    import android.os.Bundle;
    import android.content.Context;
    import android.graphics.Color;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    import android.widget.LinearLayout.LayoutParams;
    import android.widget.TextView;
    
    public class MainActivity extends SherlockFragmentActivity {
      // Consistent fragment instance
        myFragment myFrag = null;
    
        // Views
        FrameLayout fl = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
    
            Button b = new Button(this);
            b.setText("Repeat");
            b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    // Reattach the same fragment
                    getSupportFragmentManager().beginTransaction().replace(fl.getId(), myFrag).commit();
    
                }
            });
    
            fl = new FrameLayout(this);
            fl.setId(200);
            fl.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    
            myFrag = new myFragment();
            getSupportFragmentManager().beginTransaction().add(fl.getId(), myFrag).commit();
    
            ll.addView(b);
            ll.addView(fl);
    
            setContentView(ll);
        }
    
        public static class myFragment extends Fragment {
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                TextView tv = new TextView(getActivity());
                tv.setText("My fragment");
                tv.setGravity(Gravity.CENTER);
                tv.setBackgroundColor(Color.RED);
    
                return tv;
            }
        }
    
    }
    

提交回复
热议问题