How to replace a Fragment on button click of that fragment?

后端 未结 6 1031
南笙
南笙 2020-11-30 09:00

I have an activity containing multiple fragments. Activity initially have fragment and in it have two buttons. Upon clicking this button I have to replace the fragment by ne

相关标签:
6条回答
  • 2020-11-30 09:29

    you can replace fragment by FragmentTransaction.

    Here you go.

    Make an interface.

    public interface FragmentChangeListener 
    {
        public void replaceFragment(Fragment fragment); 
    }
    

    implements your Fragment holding activity with this interface.

    public class HomeScreen extends FragmentActivity implements
            FragmentChangeListener {
    
    
             @Override
             public void replaceFragment(Fragment fragment) {
                FragmentManager fragmentManager = getSupportFragmentManager();;     
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(mContainerId, fragment, fragment.toString());
                fragmentTransaction.addToBackStack(fragment.toString());
                fragmentTransaction.commit();   
        }
    
    }
    

    Call this method from Fragments like this.

    //In your fragment.

    public void showOtherFragment()
    {
           Fragment fr=new NewDisplayingFragment();
                 FragmentChangeListener fc=(FragmentChangeListener)getActivity();
                 fc.replaceFragment(fr);
    }
    

    Hope this will work!

    NOTE: mContainerId is id of the view who is holding the fragments inside. You should override Fragment's onString() method as well.

    0 讨论(0)
  • 2020-11-30 09:38
    1. Define an interface and call it IChangeListener (or something like that) and define a method inside which will do the work (ex, changeFragment()) (or call another method which will do the work) for changing the fragment).
    2. Make your activity implement the interface, or make an anonymous class within the activity implement it.
    3. Make a paramerized constructor of your fragment, which accepts a IChangeListener parameter.
    4. When initializing the fragment, pass your IChangeListener implementation (the anonymous class or the activity, implementing it)
    5. Make the onClick listener of the button call the changing method of the IChangeListener.
    0 讨论(0)
  • 2020-11-30 09:41

    You can use the following to replace a fragment on button click of that fragment:

    getFragmentManager().beginTransaction().replace(R.id.main_content, new insertFragmentNameHere()).addToBackStack(null).commit();
    
    0 讨论(0)
  • 2020-11-30 09:42

    you can try this code it's work fine with me , inflate the layout to view , Define the buton and on click ,

    Button btn_unstable;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home,container,false);
    
        btn_unstable = (Button)view.findViewById(R.id.btn_unstable);
        btn_unstable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_replace, new UnstableFragment()).commit();
    
            }
        });
        return view;
    }
    
    0 讨论(0)
  • 2020-11-30 09:49

    Well even I am learning android...

    I solved same problem recently, "How to Change Fragment On button's click event".

    buttonName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentTransaction fragmentTransaction = getActivity()
                .getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame1, new Homefragment());
            fragmentTransaction.commit();
        }
    });
    

    Here frame1 is id of FrameLayout which have define in my DrawerLayer's XML.

    So now whenever I want fragment transaction I use this code. Each time it will replace frame1 instated of your last fragment.

    FragmentTransaction fragmentTransaction = getActivity()
            .getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame1, new newfragment());
    fragmentTransaction.commit()
    

    Hope this will help..

    0 讨论(0)
  • 2020-11-30 09:50

    From the future 2017 and after, there exists different libraries that triggers Events using Bus and now you can use it to tell the activity when an event is trigger in a fragment that it owns.

    Refer to:

    RxBus with RxJava

    You can check new architectures suggested by Google

    ViewModel

    Don't use the approach in the accepted answer, get really ugly with more than 3 different events from fragments

    0 讨论(0)
提交回复
热议问题