Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find fragment by tag

I'm using support package v4.

mFragmentManager = getSupportFragmentManager();

FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.add(R.id.fragContainer1, new ModelListFragment(), FRAG_MODEL_LIST);
ft.add(R.id.fragContainer2, new TrimListFragment(), FRAG_TRIM_LIST);
ft.commit();

Fragment fragment = mFragmentManager.findFragmentByTag(
            MainActivity.FRAG_MODEL_LIST);
Log.d("MY", "found fragment: " + (fragment != null));

Always returns "found fragment: false". I'm missing something really obvious here, what is it?

like image 821
Indrek Kõue Avatar asked Aug 09 '12 12:08

Indrek Kõue


1 Answers

As you can see in doc:

The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.

that's why you doesn't see your fragment just after commit().

like image 102
ania Avatar answered Oct 06 '22 18:10

ania