As you can see in the code below I can send data from one Fragment to another and also from a Fragment to Activity but I have tried everything to send from Activity to Fragment but I can not:
MainActivity.java
public class MainActivity extends AppCompatActivity {
String TabFragmentB;
String TabFragmentA;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "One");
adapter.addFrag(new TwoFragment(), "Two");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
////Métodos que se relacionam com os fragments para trasporte de dados////
//Captura a Tag do fragment OneFragment
public void setTabFragmentA(String t){
TabFragmentA = t;
}
//Retorna a Tag do fragment OneFragment
public String getTabFragmentA(){
return TabFragmentA;
}
//Captura a Tag do fragment TwoFragment
public void setTabFragmentB(String t){
TabFragmentB = t;
}
//Retorna a Tag do fragment TwoFragment
public String getTabFragmentB(){
return TabFragmentB;
}
//Envia dados para o fragment OneFragment
public void msg_fragA(String message){
}
}
OneFragment.java
public class OneFragment extends Fragment {
static TextView statusMessage;
static TextView txt_temporizador;
static TextView txt_limite;
static TextView txt_espera;
static TextView txt_clock;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Infla o layout do fragment
View myFragmentView = inflater.inflate(R.layout.fragment_one, container, false);
String myTag = getTag();//Grava a Tag do fragment
((MainActivity) getActivity()).setTabFragmentA(myTag);//Grava a Tag do fragment em uma váriavel da MainActivity
statusMessage = (TextView) myFragmentView.findViewById(R.id.statusMessage);
txt_temporizador = (TextView) myFragmentView.findViewById(R.id.txt_temporizador);
txt_limite = (TextView) myFragmentView.findViewById(R.id.txt_limite);
txt_espera = (TextView) myFragmentView.findViewById(R.id.txt_espera);
txt_clock = (TextView) myFragmentView.findViewById(R.id.txt_clock);
return myFragmentView;
}
public void dados_update(String tp, String tl, String te, String cl) {
//Esse metódo recebe dados do TwoFragment ao ser apertado o botão envia do mesmo
txt_temporizador.setText(tp);
txt_limite.setText(tl);
txt_espera.setText(te);
txt_clock.setText(cl);
}
}
TwoFragment.java
public class TwoFragment extends Fragment {
Button btn_envia;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Infla o layout do fragment
View myFragmentView = inflater.inflate(R.layout.fragment_two, container, false);
String myTag = getTag();//Grava a Tag do fragment
((MainActivity) getActivity()).setTabFragmentB(myTag);//Grava a Tag do fragment em uma váriavel da MainActivity
btn_envia = (Button) myFragmentView.findViewById(R.id.button_enviar);
btn_envia.setOnClickListener(Envia_dados);
return myFragmentView;
}
//Instruções que serão seguidas quando o botão enviar for clicado
View.OnClickListener Envia_dados
= new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String TabOfFragmentA = ((MainActivity) getActivity()).getTabFragmentA();//Captura a Tag do fragment OneFragment
OneFragment fragmentA = (OneFragment) getActivity()
.getSupportFragmentManager()
.findFragmentByTag(TabOfFragmentA);
fragmentA.dados_update("ON", "00:00:01", "00:00:02", "00:00:03");//Envia os dados para o OneFragment
Toast.makeText(getActivity(),"Dados enviados!",Toast.LENGTH_LONG).show();
}
};
}