The royal family name was Braganca and they were reigning in Portugal since 1640. The regent was Joao Vi because his mother, Maria I was mad at the time that Napoleon Bonaparte was going to invade Portugal and imprison the royal family, but fortunately, Portugal had a secret alliance with England (enemy of Napoleon) that helped the family to escape to Brazil witn some financial help. When they first arrived in Rio de Janeiro in 1808, people were amazed, because never ever a royal family menber have visisted brazil before. It was a major change in the routine and habits of this society. What happened next is that there were made changes in the city to accomodate not only the royal family but the whole aristocracy that fled within. When Napoleon finally invaded Portugal, he was furious, because Lisbon was a famine city where all the richness and money had fled to Brazil.
Joao was married to the Spanish princess Carlota Joaquina since 1785 and had several children. In 1816 Maria died and finally Joao became king but the same year he was forced to create a kind of United Kingdom of Brazil and Portugal. The next year, his eldest son, Pedro married Archduchess Leopoldine of Austria.
In 1821 within political complications in Portugal mainly because of the absence of the royal family for more than 13 years, Joao and his family had finally to return to Lisbon (much to his dismay, because he fell in love with Brazil and left Rio de Janeiro completely heartbroken). Hel left his son Pedro as regent in Brazil and now comes the complicated bit, because history books says that Pedro (influenced by liberal politics) decided to declare Brazil independent on 7 September 1822, but what really happened according to proven documents was that Joao (his father) was well aware of the fact and completely agreed with it. Of course England was behind the scenes making the best of it.
Well, Pedro in Brazil had several children, including the eldest Maria da Gloria and the youngest, Pedro (future Pedro II). Pedro was crowned Pedro I of Brazil, but in 1826 his father dies and he engines a plan to keep both countries in the family: he send his daughter (7 years of age) to Lisbon to contract marriage with her uncle, Prince Miguel (18 years her senior), but influenced by his mother, Carlota Joaquina, Miguel decided not to marry his niece and insted declared himself king of Portugal.
Pedro in Brazil was forced then to leave Brazil and his 5 year old son (that only happened in 1830, 4 years later) behind and for the next 4 years he fought his own brother in what was called the Miguelist war (1830-1834) with the result of Miguel being expelled (he fled to Austria) and Pedro's final victory, but dying of tuberculosis the same year. So, his daugther was crowned Maria II in 1834.
I hope I helped to clear your questions.