I am building a tablet app. In this app there is one activity with two fragments. First fragment is a \"known\" list fragment which is showing a simple one item layout list
In Android API 24 FragmentTransaction
has synchronous .commitNow()
method.
It's in the reference now: https://developer.android.com/reference/android/app/FragmentTransaction.html#commitNow()
On the contrary, .commit()
works asynchronously. It just schedules a commit of the transaction.
Try running fragmentManager.executePendingTransactions()
after committing your transaction but before finding by tag and see if that works for you.
I was facing a similar issue.
I think the key learning here is using commitNow() instead of commit() with getSupportFragmentManager. This will disallow the main thread from executing until the fragment has been destroyed. It is imperative when building interfaces and using a shared activity. I should know it had me stumped for a while!
Here is a sample code example: getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.fragment_frame)).commitNow();
"....so I can do some work with the added fragment? Furthermore is there any way to wait until the commit happens and then continue with the rest of the code?"
It all depends on what work you want to do. From your question I see that most of your work code should be in your fragment code anyway, for example when an inventory item is selected.
On the callback when a selection list item is selected (in order to change the details fragment) you'll be able to get hold of the details fragment comfortably enough anyway.
Furthermore, you already have the fragment from the return of FragmentsPool.getHelperFileFragment(501),
so I don't see why you need to get the fragment via its tag.
I'm interested to know what work you need to do in onCreate
with your added details fragment.