Such a problem, by pressing the search button, I return a list, under which the layout's are created on the fragment. I try to make the fragment reload each time the button is pressed so that the output is from scratch each time. The trouble is that when using detach
and attach
list is not displayed at all. That is, it appears for a split second and then the fragment is “cleaned”, I can’t even catch at what point the list is displayed and disappears, because it happens not inside the method (here I apparently don’t understand something at all). The method looks like this:
public void searchRestaurantButtonClick(View view) { final FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.detach(fragment); ft.attach(fragment); ft.commit(); EditText searchEditText = (EditText) findViewById(R.id.searchEditText); String searchText = searchEditText.getText().toString(); List<Restaurant> restaurantsList = getRestaurantsList(searchText); LinearLayout verticalLayout = (LinearLayout) findViewById(R.id.newsFragment); for (Restaurant each : restaurantsList) { LinearLayout horizontalLayout = new LinearLayout(this); TextView titleTextView = new TextView(this); titleTextView.setText(each.getTitle()); horizontalLayout.addView(titleTextView); verticalLayout.addView(horizontalLayout); } }
If the block with FragmentTransaction
commented out, the list is returned and displayed normally, but, of course, when you re-request it remains there, and the result of the new request (the following list) is displayed under the previous list. Can someone explain the trick? I also tried to do it via replace
, but then the query returns an error:
Caused by: java.lang.IllegalStateException: Can't change container ID of fragment FragmentNews{2abadd29 #0 id=0x7f0a0050}: was 2131361872 now 2131361880