I create my first application. Short sense: displays a list of devices with related information, when you click on the list item - opens detailed information on the selected device. Development began with the display of detailed information. DetailActivity contains inside itself a Fragment, which in the xml markup activity is placed with the line:

<include layout="@layout/content" /> 

Content of the content.xml file itself:

 <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment" android:name="com.example.user.labtmp.DetailDeviceFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:layout="@layout/fragment_detail_device" /> 

Wrote the display of detailed information and began to display ListView in the existing fragment. In the manifest I registered a launcher for a new ListViewActivity, which I tied to the XML representation DetailActivity.xml. New activation code:

 public class ListViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragment); if (fragment==null) { fragment = new ListViewFragment();// Фрагмент с отображением списка fm.beginTransaction(). add(R.id.fragment, fragment) .commit(); } } } 

I thought that the fragment manager should replace the line Content.xml:

 android:name="com.example.user.labtmp.DetailDeviceFragment" 

on our fragment. But the start shows that DetailDeviceFragment is displayed instead of the ListViewFragment. If manually changing the content to our fragment, then everything is displayed correctly. There are two questions connected with this: 1. Is there really no way to change the content.xml programmatically and you have to write your ContentXFragment.xml for each fragment? 2. Why doesn't the Fragment manager change the Content.xml?

UPD. Removed a line with the name tag from content.xml. Now the fragment has no binding to the layout. Replaced the add method with the replace method of the FragmentManager. Deduced a log on methods onAttach, onCreateView, onCreate. In the emulator, the application does not start. Even the onAttach method is not recorded in the logs.

    1 answer 1

    The documentation on the ListFragment says: "To do this, your view of the hierarchy must contain a list of objects with the id" @android: id / list "(or list if it's in code). Therefore, I changed the code of my include (content.xml) in accordance with this requirement. I added the ConstraintLayout container and inserted a ListView with a line into it:

     android:id="@id/android:list" 

    I transferred the ConstraintLayout id to the adapter:

     fm.beginTransaction(). add(R.id.frameContainer, fragment) .commit(); 

    after that, everything worked as it should be.