My Idea is that $A \cap B$ cant be empty, otherwise, $A \cup B$ would be the union of two disjoint sets and thus not connected.
So $A \cup B$ is connected, which means the continuous function to $\{0,1\}$ is constant
$\implies f: A \to \{0,1\}$ and $f:B\to \{0,1\}$ is constant, which means they are both connected.
But now I didn't use the fact that A and B are closed and I have the feeling I am missing something.
Let $f:A\to\{0,1\}$ be a continuous function. Then, the restriction of $f$ to $A\cap B$ is a constant function, because $A\cap B$ is connected. Let's say that $f$ is $0$ on the intersection. Then, take $g$ the function $A\cup B\to\{0,1\}$ which is $f$ on $A$ and $0$ on $B$. It is well defined because $f$ and $0$ coincide on the intersection. It is continuous because it is continuous on $A$, on $B$, and $A$ and $B$ are closed (classical lemma). So, $g$ is constant because $A\cup B$ is connected, and so $f$ is constant. Hence the result.