简体   繁体   中英

Open fragment with button from another fragment

I want to open another fragment using a button from another fragment.

This is the fragment with the button

 @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_myday, container, false);
        Button buttonNext = (Button)view.findViewById(R.id.next);
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment someFragment = new TomorrowFragment();
                FragmentTransaction fr = getFragmentManager().beginTransaction();
                fr.replace(R.id.fragment_container, someFragment);
                fr.commit();

            }
        });

        return view;

Fragment xml

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next to 2"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"
        android:id="@+id/next"/>

after I press the button nothing happens. I have followed code from other posts but nothing worked..

buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment someFragment = new TomorrowFragment();
                FragmentTransaction fr = getSupportFragmentManager().beginTransaction();
                fr.replace(R.id.fragment_container, someFragment);
                fr.commitAllowingStateLoss();

            }
        });

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