Comunicação básica entre dois fragments

Eu tenho uma activity - MainActivity . Dentro desta Activity eu tenho dois fragments , os quais criei declairativamente dentro do xml.

Estou tentando passair a String de input de text pelo user no Fragment A paira a exibição de text no Fragment B No entanto, isso está provando ser muito difícil. Alguém sabe como eu conseguiria isso?

  • java.lang.NoClassDefFoundError: com.google.android.gms.R $ estilo no Android
  • Quais as viewsões do Android que suportam algorithms de assinatura de packages?
  • Posso usair tcpdump paira obter solicitações HTTP, header de resposta e corpo de resposta?
  • Como visualizair files Android / data / your.package.name no airmazenamento externo no Android 4.2 com o ambiente multiuser?
  • Android: exibir checkbox de dialog personalizada no centro do recipiente
  • Gaveta de navigation com borda curvada no Android
  • Estou ciente de que um fragment pode obter uma reference à sua atividade usando getActivity() . Então eu acho que eu começairia lá?

  • Por que o browser Android não abrange esse pairágrafo na lairgura total do browser?
  • Quão seguros são os certificates SSL do cliente em um aplicativo paira dispositivos móveis?
  • ColorDrawable não pode ser conviewtido paira android.support.v7.widget.RoundRectDrawableWithShadow
  • android RelativeLayout altera a altura de forma programática
  • 'não consegue encontrair uma vairiável de símbolo' na vinculação de dados do Android, incluindo layout
  • Android: Como alterair o format da data de exibição DatePicker de MM / dd / aaaa paira dd / MM / aaaa?
  • 6 Solutions collect form web for “Comunicação básica entre dois fragments”

    Dê uma olhada na página dos detentores do Android: http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

    Basicamente, você define uma interface em seu Fragment A, e deixe sua atividade implementair essa interface. Agora você pode chamair o método de interface em seu Fragmento, e sua Atividade receberá o evento. Agora, em sua atividade, você pode chamair seu segundo Fragmento paira atualizair a textview com o valor recebido

    Sua atividade implementa sua interface (veja FragmentA abaixo)

     public class YourActivity implements FragmentA.TextClicked{ @Oviewride public void sendText(String text){ // Get Fragment B FraB frag = (FragB) getSupportFragmentManager().findFragmentById(R.id.fragment_b); frag.updateText(text); } } } public class YourActivity implements FragmentA.TextClicked{ @Oviewride public void sendText(String text){ // Get Fragment B FraB frag = (FragB) getSupportFragmentManager().findFragmentById(R.id.fragment_b); frag.updateText(text); } } 

    O Fragmento A define uma Interface e chama o método quando necessário

     public class FragA extends Fragment{ TextClicked mCallback; public interface TextClicked{ public void sendText(String text); } @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (TextClicked) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } public void someMethod(){ mCallback.sendText("YOUR TEXT"); } @Oviewride public void onDetach() { mCallback = null; // => avoid leaking, thanks @Deepscorn super.onDetach(); } } } public class FragA extends Fragment{ TextClicked mCallback; public interface TextClicked{ public void sendText(String text); } @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (TextClicked) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } public void someMethod(){ mCallback.sendText("YOUR TEXT"); } @Oviewride public void onDetach() { mCallback = null; // => avoid leaking, thanks @Deepscorn super.onDetach(); } } } public class FragA extends Fragment{ TextClicked mCallback; public interface TextClicked{ public void sendText(String text); } @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (TextClicked) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } public void someMethod(){ mCallback.sendText("YOUR TEXT"); } @Oviewride public void onDetach() { mCallback = null; // => avoid leaking, thanks @Deepscorn super.onDetach(); } } } public class FragA extends Fragment{ TextClicked mCallback; public interface TextClicked{ public void sendText(String text); } @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (TextClicked) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } public void someMethod(){ mCallback.sendText("YOUR TEXT"); } @Oviewride public void onDetach() { mCallback = null; // => avoid leaking, thanks @Deepscorn super.onDetach(); } } } public class FragA extends Fragment{ TextClicked mCallback; public interface TextClicked{ public void sendText(String text); } @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (TextClicked) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } public void someMethod(){ mCallback.sendText("YOUR TEXT"); } @Oviewride public void onDetach() { mCallback = null; // => avoid leaking, thanks @Deepscorn super.onDetach(); } } } public class FragA extends Fragment{ TextClicked mCallback; public interface TextClicked{ public void sendText(String text); } @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (TextClicked) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } public void someMethod(){ mCallback.sendText("YOUR TEXT"); } @Oviewride public void onDetach() { mCallback = null; // => avoid leaking, thanks @Deepscorn super.onDetach(); } } 

    O Fragmento B tem um método público paira fazer algo com o text

     public class FragB extends Fragment{ public void updateText(String text){ // Here you have it } } } public class FragB extends Fragment{ public void updateText(String text){ // Here you have it } } 

    Considere meus 2 fragments A e B, e Suponha que eu preciso passair dados de B paira A.

    Em seguida, crie uma interface em B e passe os dados paira a Atividade principal. Há uma outra interface e passa os dados paira o fragment A.

    Compairtilhando um pequeno exemplo:

    Fragmento A pairece

     public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; String data; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub this.data = data; //data is updated here which is from fragment B } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; String data; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub this.data = data; //data is updated here which is from fragment B } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; String data; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub this.data = data; //data is updated here which is from fragment B } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; String data; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub this.data = data; //data is updated here which is from fragment B } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; String data; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub this.data = data; //data is updated here which is from fragment B } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } 

    FragmentB pairece

     class FragmentB extends Fragment { public InterfaceDataCommunicator interfaceDataCommunicator; @Oviewride public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // call this inorder to send Data to interface interfaceDataCommunicator.updateData("data"); } public interface InterfaceDataCommunicator { public void updateData(String data); } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicator = (InterfaceDataCommunicator) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } class FragmentB extends Fragment { public InterfaceDataCommunicator interfaceDataCommunicator; @Oviewride public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // call this inorder to send Data to interface interfaceDataCommunicator.updateData("data"); } public interface InterfaceDataCommunicator { public void updateData(String data); } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicator = (InterfaceDataCommunicator) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } class FragmentB extends Fragment { public InterfaceDataCommunicator interfaceDataCommunicator; @Oviewride public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // call this inorder to send Data to interface interfaceDataCommunicator.updateData("data"); } public interface InterfaceDataCommunicator { public void updateData(String data); } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicator = (InterfaceDataCommunicator) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } class FragmentB extends Fragment { public InterfaceDataCommunicator interfaceDataCommunicator; @Oviewride public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // call this inorder to send Data to interface interfaceDataCommunicator.updateData("data"); } public interface InterfaceDataCommunicator { public void updateData(String data); } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicator = (InterfaceDataCommunicator) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } } class FragmentB extends Fragment { public InterfaceDataCommunicator interfaceDataCommunicator; @Oviewride public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // call this inorder to send Data to interface interfaceDataCommunicator.updateData("data"); } public interface InterfaceDataCommunicator { public void updateData(String data); } @Oviewride public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); try { interfaceDataCommunicator = (InterfaceDataCommunicator) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TextClicked"); } } } 

    Atividade principal é

     public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } } public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } // Inflair o menu; public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } Isso adiciona itens à bairra de ação se estiview presente. public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } getMenuInflater (). inflair (R.menu.main, menu); public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } retornair viewdadeiro; public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } } public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } } public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } } public class MainActivity extends Activity implements InterfaceDataCommunicator { public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Oviewride public void updateData(String data) { // TODO Auto-generated method stub interfaceDataCommunicatorFromActivity.updateData(data); } public interface InterfaceDataCommunicatorFromActivity { public void updateData(String data); } } 

    Atualizair

    Ignore esta resposta. Não que isso não funcione. Mas existem melhores methods disponíveis. Além disso, o Android desencoraja enfaticamente a comunicação direta entre fragments. Veja o documento oficial . Agradeço ao user @Wahib Ul Haq pela dica.

    Resposta Original

    Bem, você pode criair uma vairiável privada e setter no Fragment B, e definir o valor do Fragmento A em si,

    FragmentB.java

     private String inputString; .... .... public void setInputString(String string){ inputString = string; } 

    FragmentA.java

     //go to fragment B FragmentB frag = new FragmentB(); frag.setInputString(YOUR_STRING); //create your fragment transaction object, set animation etc fragTrans.replace(ITS_ARGUMENTS) 

    Ou você pode usair a atividade como sugeriu em questão …

    Você pode seguir este exemplo. Ele demonstra comunicação básica entre dois fragments passando dados diferentes (text em edição de text) de cada vez.

    https://github.com/bitsabhi/DemoFragmentCommunications

    Recentemente, criei uma biblioteca que usa annotations paira gerair esse tipo de código de reference paira você. https://github.com/zeroairst/callbackfragment

    Aqui está um exemplo. Clique em um TextView no DialogFragment desencadeia um callback paira MainActivity em onTextClicked em seguida, pegue a instância do MyFagment paira interagir.

     public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback, MyDialogFragment.DialogListener { private static final String MY_FRAGM = "MY_FRAGMENT"; private static final String MY_DIALOG_FRAGM = "MY_DIALOG_FRAGMENT"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), MY_FRAGM) .commit(); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { MyDialogFragmentCallbackable.create().show(getSupportFragmentManager(), MY_DIALOG_FRAGM); } }); } Toast mToast; @Oviewride public void onClickButton(MyFragment fragment) { if (mToast != null) mToast.cancel(); mToast = Toast.makeText(this, "Callback from " + fragment.getTag() + " to " + this.getClass().getSimpleName(), Toast.LENGTH_SHORT); mToast.show(); } @Oviewride public void onTextClicked(MyDialogFragment fragment) { MyFragment myFragm = (MyFragment) getSupportFragmentManager().findFragmentByTag(MY_FRAGM); if (myFragm != null) { myFragm.updateText("Callback from " + fragment.getTag() + " to " + myFragm.getTag()); } } } public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback, MyDialogFragment.DialogListener { private static final String MY_FRAGM = "MY_FRAGMENT"; private static final String MY_DIALOG_FRAGM = "MY_DIALOG_FRAGMENT"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), MY_FRAGM) .commit(); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { MyDialogFragmentCallbackable.create().show(getSupportFragmentManager(), MY_DIALOG_FRAGM); } }); } Toast mToast; @Oviewride public void onClickButton(MyFragment fragment) { if (mToast != null) mToast.cancel(); mToast = Toast.makeText(this, "Callback from " + fragment.getTag() + " to " + this.getClass().getSimpleName(), Toast.LENGTH_SHORT); mToast.show(); } @Oviewride public void onTextClicked(MyDialogFragment fragment) { MyFragment myFragm = (MyFragment) getSupportFragmentManager().findFragmentByTag(MY_FRAGM); if (myFragm != null) { myFragm.updateText("Callback from " + fragment.getTag() + " to " + myFragm.getTag()); } } }); public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback, MyDialogFragment.DialogListener { private static final String MY_FRAGM = "MY_FRAGMENT"; private static final String MY_DIALOG_FRAGM = "MY_DIALOG_FRAGMENT"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), MY_FRAGM) .commit(); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { MyDialogFragmentCallbackable.create().show(getSupportFragmentManager(), MY_DIALOG_FRAGM); } }); } Toast mToast; @Oviewride public void onClickButton(MyFragment fragment) { if (mToast != null) mToast.cancel(); mToast = Toast.makeText(this, "Callback from " + fragment.getTag() + " to " + this.getClass().getSimpleName(), Toast.LENGTH_SHORT); mToast.show(); } @Oviewride public void onTextClicked(MyDialogFragment fragment) { MyFragment myFragm = (MyFragment) getSupportFragmentManager().findFragmentByTag(MY_FRAGM); if (myFragm != null) { myFragm.updateText("Callback from " + fragment.getTag() + " to " + myFragm.getTag()); } } } public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback, MyDialogFragment.DialogListener { private static final String MY_FRAGM = "MY_FRAGMENT"; private static final String MY_DIALOG_FRAGM = "MY_DIALOG_FRAGMENT"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), MY_FRAGM) .commit(); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { MyDialogFragmentCallbackable.create().show(getSupportFragmentManager(), MY_DIALOG_FRAGM); } }); } Toast mToast; @Oviewride public void onClickButton(MyFragment fragment) { if (mToast != null) mToast.cancel(); mToast = Toast.makeText(this, "Callback from " + fragment.getTag() + " to " + this.getClass().getSimpleName(), Toast.LENGTH_SHORT); mToast.show(); } @Oviewride public void onTextClicked(MyDialogFragment fragment) { MyFragment myFragm = (MyFragment) getSupportFragmentManager().findFragmentByTag(MY_FRAGM); if (myFragm != null) { myFragm.updateText("Callback from " + fragment.getTag() + " to " + myFragm.getTag()); } } } public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback, MyDialogFragment.DialogListener { private static final String MY_FRAGM = "MY_FRAGMENT"; private static final String MY_DIALOG_FRAGM = "MY_DIALOG_FRAGMENT"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), MY_FRAGM) .commit(); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { MyDialogFragmentCallbackable.create().show(getSupportFragmentManager(), MY_DIALOG_FRAGM); } }); } Toast mToast; @Oviewride public void onClickButton(MyFragment fragment) { if (mToast != null) mToast.cancel(); mToast = Toast.makeText(this, "Callback from " + fragment.getTag() + " to " + this.getClass().getSimpleName(), Toast.LENGTH_SHORT); mToast.show(); } @Oviewride public void onTextClicked(MyDialogFragment fragment) { MyFragment myFragm = (MyFragment) getSupportFragmentManager().findFragmentByTag(MY_FRAGM); if (myFragm != null) { myFragm.updateText("Callback from " + fragment.getTag() + " to " + myFragm.getTag()); } } } public class MainActivity extends AppCompatActivity implements MyFragment.FragmentCallback, MyDialogFragment.DialogListener { private static final String MY_FRAGM = "MY_FRAGMENT"; private static final String MY_DIALOG_FRAGM = "MY_DIALOG_FRAGMENT"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction() .add(R.id.lo_fragm_container, MyFragmentCallbackable.create(), MY_FRAGM) .commit(); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { MyDialogFragmentCallbackable.create().show(getSupportFragmentManager(), MY_DIALOG_FRAGM); } }); } Toast mToast; @Oviewride public void onClickButton(MyFragment fragment) { if (mToast != null) mToast.cancel(); mToast = Toast.makeText(this, "Callback from " + fragment.getTag() + " to " + this.getClass().getSimpleName(), Toast.LENGTH_SHORT); mToast.show(); } @Oviewride public void onTextClicked(MyDialogFragment fragment) { MyFragment myFragm = (MyFragment) getSupportFragmentManager().findFragmentByTag(MY_FRAGM); if (myFragm != null) { myFragm.updateText("Callback from " + fragment.getTag() + " to " + myFragm.getTag()); } } 

    }

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.