  The exotic states $X(3872)$ and $Z_c(3900)$ have long been conjectured as
isoscalar and isovector $\bar{D}^*D$ molecules. In this work, we first propose
the triangle diagram mechanism to investigate their productions in $B$ decays
as well as their heavy quark spin symmetry partners, $X_2(4013)$ and
$Z_c(4020)$. We show that the large isospin breaking of the ratio
$\mathcal{B}[B^+ \to X(3872) K^+]/\mathcal{B}[B^0 \to X(3872) K^0] $ can be
attributed to the isospin breaking of the neutral and charged $\bar{D}^*D$
components in their wave functions. For the same reason, the branching
fractions of $Z_c(3900)$ in $B$ decays are smaller than the corresponding ones
of $X(3872)$ by at least one order of magnitude, which naturally explains its
non-observation. A hierarchy for the production fractions of $X(3872)$,
$Z_c(3900)$, $X_2(4013)$, and $Z_c(4020)$ in $B$ decays, consistent with all
existing data, is predicted. Furthermore, with the factorization ansatz we
extract the decay constants of $X(3872)$, $Z_c(3900)$, and $Z_c(4020)$ as
$\bar{D}^*D^{(*)}$ molecules via the $B$ decays, and then calculate their
branching fractions in the relevant $B_{(s)}$ decays, which turn out to agree
with all existing experimental data. The mechanism we proposed is useful to
elucidate the internal structure of the many exotic hadrons discovered so far
and to extract the decay constants of hadronic molecules,which can be used to
predict their production in related processes.
