I have an activity which has two fragments
When I launch the app, I want the fragment one to be displayed.
What code should I have in my MainActivity to do so?
Write following code in you activity's onCreate() method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentOne fragmentOne = new FragmentOne();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragmentOne, "fragmentOne")
.addToBackStack("fragmentOne")
.commit();
}
Without having any additional information, I can say that FragmentManager has a hide and show function that may be helpful during activities.