public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager=getFragmentManager(); //perform fragment transaction here } }
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Add Fragment to Activity FragmentManager fragmentManager=getFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); MyFragment myFragment=new MyFragment(); fragmentTransaction.add(R.id.fragment_container,myFragment); fragmentTransaction.commit(); } }In these examples, we have used the android.app.Activity package library which contains the FragmentManager class. The getFragmentManager method returns a FragmentManager object that can be used to add, replace or remove fragments.