Let $X$ be a Metric Space, and let $\{A_i\}_{i=1}^{\infty}$ be a countable collection of non-empty subsets of $X$ whose union is $X$. Let $f : X \rightarrow \mathbb{R}$ be a function such that the restriction of $f$ to $A_i$ is continuous for each $i$.
I need to show that if each $A_i$ is open then $f$ is continuous.
So let $f(A_i) = B_i$, and we know that since $f$ is continuous on $A_i$, for each open $B'_i \subseteq B_i$, $f^{-1}(B'_i)$ is open in $X$. So take an open set $(a,b) \subset \mathbb{R}$, and the preimage is either the nullset in $X$, or, $(a,b) \subseteq \cup_{i=1}^{\infty}B_i$, and since $f^{-1}f(U) \subseteq U$, that set is contained in the union of all $A_i$, which is open.
Am I on a right track for a proof and if so is there something I can improve on? If not, what would be the right track to go?
This does not holds if the $A_i$ are not open. For example, let $X=\mathbb{R},$ $A_0=(-\infty,0],$ and $A_i=[1/i, \infty)$ if $i \geq 1.$ Then each $A_i$ is not open in $\mathbb{R}$ and $\mathbb{R}= \bigcup_i A_i.$ Let \begin{equation*} \begin{aligned} f : \mathbb{R} & \to \mathbb{R} \\ x &\mapsto \begin{cases}0 &\text{if } x \in A_i\text{ for some } i \geq 1, \\ 1&\text{if } x \in A_0. \end{cases} \end{aligned} \end{equation*} Then $f|A_i$ is continuous for each $i,$ but $f$ is not continuous at 0.
So suppose each $A_i$ is open in $X.$ For each $i=0,1,2,\ldots$ let $f_i=f|_{A_i}:A_i\to\mathbb{R}$ denote the restriction of $f$ to the set $A_i.$ By hypothesis each $f_i$ is continuous.
Given $(a,b)\subset\mathbb{R},$ note that $$ f^{-1}(a,b)=\bigcup_{i=1}^\infty f_i^{-1}(a,b).$$ Each set $f_i^{-1}(a,b)$ is open in $A_i$ by continuity of $f_i,$ but each $A_i$ is open in $X,$ so each $f_i^{-1}(a,b)$ is open in $X.$