Android FragmentTransaction commit When?

I am creating a tablet application. In this application, there is one action with two fragments. The first fragment is a โ€œknownโ€ fragment of the list, which shows a simple list of layouts of elements from the database query, the second fragment shows the details of the selected record (from the list fragment). The thought with the second fragment is that its type depends on the entries displayed in the list. For example, if the records are customers, then the selected customer data is displayed, if they are inventory items, the data of the selected item is displayed, etc. To contact the fragment of parts, I created an interface that implements each fragment of the fragment. A fragment of the list is "fixed" in the activity from the xml layout. However, a part fragment is created during activity creation as follows:

super.onCreate(savedInstanceState); setContentView(R.layout.act_hlpfiles_host); ..... FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.laydetailsfragment, FragmentsPool.getHelperFileFragment(501), "recordDetails"); fragmentTransaction.commit(); myDetailsFragment = getFragmentManager().findFragmentByTag("recordDetails"); ... myListFragment = (frg_hlpfiles_lstrecords) getFragmentManager().findFragmentById(R.id.frg_lstrecords); .... } 

The problem with this code is that myDetailsFragment is always null. This is because the Transaction.commit () fragment does not start immediately, but it happens in the main thread the next time the thread is ready (as indicated in the android documentation).

If I create a part fragment in onStart () and instantiate a list fragment in onCreate, everything works fine.

So the question is: how can I be sure that the Transaction.commit () fragment completed the transaction so that I can do some work with the added fragment? Also, is there a way to wait for the commit to happen and then continue with the rest of the code?

Thank you for your time and efforts,

Christ

+44
android android-fragments
Aug 30 '11 at 16:24
source share
3 answers

Try to run fragmentManager.executePendingTransactions() after the transaction, but before you find the tag and see if this works for you.

+97
Aug 30 2018-11-11T00:
source share

Android API 24 FragmentTransaction has a synchronous .commitNow() method. This is now in the link: https://developer.android.com/reference/android/app/FragmentTransaction.html#commitNow ()

Conversely, .commit() works asynchronously. He just plans to commit the transaction.

+3
Mar 12 '16 at 8:02
source share

".... so I can do some work with the added snippet . Also, is there any way to wait for the commit to happen , and then continue with the rest of the code ?"

It all depends on what kind of work you want to do. From your question, I see that most of your working code should be in the fragment code anyway, for example, when an inventory item is selected.

When you call back when a selection list item is selected (to change a part fragment), you can sufficiently get a part fragment.

In addition, you already have a fragment from the return FragmentsPool.getHelperFileFragment(501), , so I donโ€™t understand why you need to get the fragment through its tag.

I am interested to know what kind of work you need to do in onCreate with a fragment of added details.

0
Aug 31 '11 at 3:13
source share



All Articles