Summon Fragment within Fragment

Asked

Viewed 1,198 times

0

I am developing a chat. I have a Mainactivity in which I invoke a Fragment (which extend baseAdapter), where I list all contacts in a listview. By clicking on a contact, I want to open a new fragment that will represent the sms sending/receiving window.

My problem is to invoke the second Fragment (within onClickListener), is it possible/correct to invoke a Fragment already inside a Fragment? if yes, how can I get around this situation?

  • Activity should manage Fragments. The Fragment that has the contact list should inform Activity that another Fragment should be opened. Look at this reply.

2 answers

0

The answer above is correct, but the code below also works:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransacion = fragmentManager.beginTransacion();
    fragmentTransaction.replace(R.id.main_framelayout, fragment).commit();

0


An example of how to solve this situation, taking into account that as Ramaral said in the comment, the activity is responsible for exchanging the fragment inside:

//Instancia o fragmentManager que é o responsável pela troca de Fragment
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
//Instancia o fragment que você vai colocar na tela
Fragment fragment = new SeuFragment();
//Faz a transação, substituindo no frame da sua MainActivity que contem os fragmentos, o antigo pelo novo fragmento que você instanciou.
fragmentManager.beginTransaction().replace(R.id.main_framelayout, fragment).commit();

Basically this is a very simple and generic way to change the screen from a Fragment, remember that if you want to pass some information forward, just instantiate a bundle and arrow it in instantiated Fragment :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.