How to open or launch a Fragment from another Activity?

后端 未结 3 1988
执笔经年
执笔经年 2020-12-18 10:50

from the adapter of a RecyclerView which is contained in an Activity, i\'m trying to launch a fragment when an element of the RecyclerView is pressed, this is my code right

3条回答
  •  猫巷女王i
    2020-12-18 11:17

    You can not start a Fragment using Intents. Intents provide a communication between activities.

    If you want to launch a Fragment from other Fragment you have two options.

    1- You can replace or add a new fragment.

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction
                    .replace( R.id.content_frame, fragment )
                    .addToBackStack( null )
                    .commit();
    

    In this case The activity has two fragments.

    2- You can launch a new Activity that contains a Fragment.

提交回复
热议问题