In my first attempt to work with Fragments
, the method replace
gives this error The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, LM_Fragment)
I supplied the id
of the first fragment
which is lm_fragment
as the first parameter for .replace
method. is that correct? if not what parameter should I supply .replace
method with?
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
LM_Fragment lm_fragment = new LM_Fragment();
fragmentTransaction.replace(R.id.lm_fragment, lm_fragment);
}else{
PM_Fragment pm_fragment = new PM_Fragment();
fragmentTransaction.replace(R.id.pm_fragment, pm_fragment);
}
fragmentTransaction.commit();
}
MainActivity xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.fragments"
android:id="@+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragments"
android:id="@+id/pm_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />