Continuous function from metric space to R 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? 
 A: It is not necessarily that $(a,b)\subseteq\displaystyle\bigcup_{i=1}^{\infty}B_{i}$.
Actually one can write that 
\begin{align*}
f^{-1}(a,b)&=\{x\in X: a<f(x)<b\}\\
&=\bigcup_{i=1}^{\infty}\{x\in A_{i}: a<f(x)<b\}\\
&=\bigcup_{i=1}^{\infty}\{x\in A_{i}: a<f|_{A_{i}}(x)<b\}\\
&=\bigcup_{i=1}^{\infty}f|_{A_{i}}^{-1}(a,b).
\end{align*}
Now $f|_{A_{i}}^{-1}(a,b)$ is open in $A_{i}$. Since $A_{i}$ is open, $f|_{A_{i}}^{-1}(a,b)$ is open in $X$.
A: 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.$
A: This holds for general topological spaces too. Let $X$ be a topological space and $X = \cup_i A_i$ where each $A_i$ is open in $X$.  Suppose $f: X \to Y$ (where $Y$ is a topological space) is such that $f|_{A_i} : A_i \to Y$ is continuous. Then $f$ is continuous.
To show this let $U \subset Y$ be open, we need to show $f^{-1}(U)$ is open. To show $X = \cup_i A_i$ therefore
$$f^{-1}(U) = \bigcup_{i} f|_{A_i}^{-1}(U) $$
Each $f|_{A_i}$ is continuous therefore $f|_{A_i}^{-1}(U)$ is open in $A_i$. So $f|_{A_i}^{-1}(U) = A_i \cap U_i$ for some $U_i$ open in $X$ by definition of the subspace topology on $A_i$. But then $A_i$ is open so $f|_{A_i}^{-1}(U)$ is open in $X$ as a finite intersection of open sets (here openess of $A_i$ is crucial). So $f^{-1}(U)$ is open as a union of open sets.
A: One could use sequential criterion for continuity here. Suppose $\{x_{n}\}$ is a sequence in the metric space $X$ converging to $x\in X$ . Since $\{A_{i}\}$ covers the whole space there exists $A_{k}$ such that $x\in A_{k}$ . Now since $A_{k}$ is open and $x\in A_{k}$ , all but finitely many elements of the sequence lie inside $A_{k}$ . Given that the restriction of f to $A_{k}$ is continuous hence $\{f(x_{n})\}$ converges to $f(x)$ . So by sequential criterion for continuity we can conclude that f is continuous .
