There are two buttons. By pressing the first button, a fragment is dynamically added using BeginTransaction() . When you click on the second button is TextView using getFragmentManager().getFragmentById(id...).getView()... , and the text changes. The fact is that it works. But if you combine both actions in one button, then the fragment is not found, and null in getFragmentManager().getFragmentById(id...).getView() will return.

Are there any pitfalls that can cause this behavior?

    2 answers 2

    By default (and this is the recommended and correct behavior) all transactions with fragments occur asynchronously. Therefore, after calling commit at the transaction for adding a fragment, it is not added instantly and when you try to access it, it will not be in the activation stack in the activated state yet.

    You need to alter the logic of changing the text in the fragment, taking into account this behavior. In principle, trying to touch the fragment markup directly is a bad practice.

    Depending on why you change the text in the fragment, you can go in different ways.

    1. If you just need to set the text once for the fragment, transfer the data to the fragment when it is created through Fragment#setArguments(Bundle args) and then drag it into the fragment in the onCreate method through Fragment#getArguments() and set the text in the TextView in the onCreateView method of the fragment .

    2. If you need to change something by pressing a button, for example, or by setting another event, then you need to create a method in the fragment that will change the text in the text field and call this method from the activation, after receiving the fragment from FragmentManager , checking that the fragment is in the active state ( Fragment#isAdded() == true ) in order not to run into the situation of its detachment from the activation.


    If you want to call the rows completely one by one and expect that the fragment will be added synchronously and you can immediately refer to its markup, you can try commitNow() instead of commit() in the transaction of adding a fragment. In theory, this will give the desired effect. But no one knows for sure, because to do so is very bad and wrong and we never do it, so it may not work.

      How stupid. I could delete this request, but nevertheless suddenly someone will need it. Generally. After we added the fragment and made commit (), as I understood it, it is necessary “to get the code back to activation” (cut off my language), so create a stream and start it. Create a message in the stream and send it to the active handler. In the handler, if necessary in the message, follow the text change. Everything. Ps do not minus please, I do not understand much, and I came to this solution by the method of experiments. And once again I repeat, I do not delete it because it may be useful to someone. If you do not understand what kind of nonsense I have written, then at least understand exactly how to proceed.

      • Yes, you rightly suspect that this is the wrong approach. See my answer. - Yuriy SPb ♦