Note that there are some explanatory texts on larger screens.

plurals
  1. POgetSupportFragmentManager().findFragmentByTag unpredictable behaviour
    text
    copied!<p>If I'm adding fragments to my activity like this:</p> <pre><code> fragment_tags = new ArrayList&lt;String&gt;(); fragment_tags.add("user_fragment"); fragment_tags.add("memorial_fragment"); UserFragment user = new UserFragment(); MemorialFragment memorial = new MemorialFragment(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(R.id.content_frame, user, fragment_tags.get(0)); ft.add(R.id.content_frame, memorial, fragment_tags.get(1)); ft.commit(); </code></pre> <p>and then after some time I'm trying to retrieve these fragments like this:</p> <pre><code> List&lt;Fragment&gt; frg = getSupportFragmentManager().getFragments(); Fragment fragUser = getSupportFragmentManager().findFragmentByTag( "user_fragment"); Fragment fragUserID = getSupportFragmentManager().findFragmentById( R.id.user_fragment_id); Log.d("MV", "fragments size:" + (frg == null ? " null" : frg.size())); </code></pre> <p>Log.d will always return size of 2, but fragUser is sometimes null and sometimes it's the fragment I put in transaction.</p> <p>Which is more interesting is that this code:</p> <pre><code> try { Log.d("MV", "0:" + frg.get(0).toString()); Log.d("MV", "1:" + frg.get(1).toString()); } catch (NullPointerException e) { Log.d("MV", "null caught"); } </code></pre> <p>will sometimes work for both fragments and sometimes just for the first and if i do smething like this: </p> <pre><code>if(frg.get(0) == fragUser) </code></pre> <p>it will be sometimes evaluated as true, but not often.. I don't really understand this behaviour, but I need to implement switching between multiple fragments, so I need to get reference to exact fragment. Anybody? I'm trying to solve this for 6 hours now and I really dont understand how to do this. Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload