How to open or launch a Fragment from another Activity?

后端 未结 3 1987
执笔经年
执笔经年 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条回答
  •  别那么骄傲
    2020-12-18 11:20

    You cannot start a Fragment with Intents so the method that I recommend to you is :

    Fragment mFragment = null;
    mFragment = new MainFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
    

    For more information see Fragments Transactions documents

提交回复
热议问题