Joseph had been taken away from his brothers, but Judah left on his own and was living among the Canaanites for some time. He marries and has three sons, which would take a number of years. Also, as this account continues, we see that Judah stayed there until at least one of the sons, Er, was old enough that Judah sought a wife for him:
Twice in these few verses we are told that Er was Judah's firstborn. I do not know why this is being emphasized, but perhaps it is to show us that Judah's line is starting to become established among the Canaanites, in whose land he was living. His wife is a Canaanite. (v.2) Judah's third son Shelah (and probably the other two sons as well) was born in Kezib. I have to check the geography, but I get the sense that Judah is living on the border of places occupied by Israelite and Canaanite peoples, perhaps traveling back and forth between the two as he tended his flocks. [Note: Adullam is southwest of Jerusalem, and Kezib is about three miles west of that.] He seems to be somewhat removed, yet lives near enough that he accompanies his brothers when they go to Egypt to get food during the famine which is soon to envelop the surrounding territory.
Judah is certainly not the first, nor the last, to try to live in both worlds -- that of the Canaanites and also of the Israelites who worshipped God. Of course, it never works out like one planned. Er is wicked (not surprisingly, since his mother does not know God and Judah might not, either...or is at least in a state of compromise). In fact, Er is so wicked that God puts him to death.
Judah's next son Onan is not any better. In that culture, if a man died before having any offspring, his brother (or sometimes his nearest male relative, if there were no brothers) was expected to marry the widow. Their first son was considered the deceased man's son for inheritance purposes. Under this arrangement, the widow's support would also be accomplished: