简体   繁体   中英

In Android Studio, how do you switch a fragment to another fragment using java code

On activity_main I have btnToggle and a fragment_text . 在此处输入图片说明

I want to switch fragment_text to fragment_image when the user taps btnToggle . 在此处输入图片说明

private void btnToggle_OnClick() {
    //what goes here?
}

First add fragment dynamically and then replace like below:-

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
SectionDescriptionFragment bdf = new SectionDescriptionFragment();
ft.replace(R.id.book_description_fragment, bdf);
ft.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM