Android - fragmentTransaction.replace () does not work in support library 25.1.0

I am replacing FrameLayout with a fragmentTransaction.replace() using fragmentTransaction.replace() .

Markup:

 <FrameLayout android:id="@+id/articlesAppender" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> 

Replacement in Activity onCreate:

 FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); articlesFragment = (ArticlesFragment) fragmentManager.findFragmentByTag(ARTICLES_FRAGMENT_TAG); if (articlesFragment == null) { articlesFragment = new ArticlesFragment(); } fragmentTransaction.replace(R.id.articlesAppender, articlesFragment, ARTICLES_FRAGMENT_TAG); fragmentTransaction.commit(); 

ArticleFragment onCreate:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.articles_fragment, container, false); view.setVisibility(View.GONE); return view; } 

But view.setVisibility(View.GONE); does not work in the support library 25.1.0. Thus, the fragment will be displayed on the screen. If I set the visibility from articlesAppender to GONE . Therefore, it should look like this:

 <FrameLayout android:id="@+id/articlesAppender" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </FrameLayout> 

Then the fragment will not be visible on the screen, but when I try to call view.setVisibility(View.VISIBLE); later, it still doesn't work. The fragment is not yet visible.

This means the view returned by inflater.inflate(R.layout.articles_fragment, container, false); is not a real kind of fragment. But it works great in the support library 25.0.1.

So, is this an Android bug?

+6
source share
3 answers
Release Announced

. more people seem to have this problem

https://code.google.com/p/android/issues/detail?id=230191

+7
source

This seems to be the bug reported in the Gillis Haasnoot message.

I noticed that if I put replace () on remove () and add () and use commitNow (), it seems to work as expected.

Source code (does not work with 25.1.0):

 fragMngr.beginTransation() .replace(R.id.detail_container, newFrag, fragTag) .commit(); 

Work around:

 // remove old fragment Fragment oldFrag = fragMngr.findFragmentByTag(fragTag); if (oldFrag != null { fragMngr.beginTransaction().remove(oldFrag).commitNow(); } // add new fragment fragMngr.beginTransaction() .add(R.id.detail_container, newFrag, fragTag) .commitNow(); 
+2
source
  //replace fragment private void replaceFragment(final Fragment newFragment, final int containerId) { final FragmentManager fragmentManager = getFragmentManager(); final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(containerId, newFragment, newFragment.getClass().getSimpleName()); fragmentTransaction.commit(); } 

ArticleFragment onCreate:

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.articles_fragment, container, false); return view; } <FrameLayout android:id="@+id/articlesAppender" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> 

Just call this method in your MainActivity onCreate () Where

newFreagment = "The fragment you want to replace with" containerId = "Framelayout id"

-one
source

Source: https://habr.com/ru/post/1013174/


All Articles