N
N
Neonoviiwolf2019-02-08 00:16:42
Android
Neonoviiwolf, 2019-02-08 00:16:42

Why is FragmentTransaction fired in vain?

Good!
There is code in the snippet

public void onStart() {
        super.onStart();

        if (!transaction.isEmpty()) {
            transaction.commit();
        }
    }

As I understand it, after fragment_1 starts, it should be checked whether the transaction contains operations and, if so, will execute them. But if I make a transaction to fragment_2, then to fragment_1
if (!transaction.isEmpty())
will return
true
for some reason the app will crash
java.lang.IllegalStateException: Fragment already added:

At the same time, in order for the application to crash, it is necessary to make a transaction in one fragment_x, and then in any other

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-02-08
@Neonoviiwolf

In general, I would like to see more code. You are doing something wrong. Why is there some transaction in the fragment's onStart already? A typical way to use transactions is fm.beginTransaction().add()....commit(). That is, you do not need to store the transaction in a class field, and even more so, you do not need to transfer it somewhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question