I have an application that consists of 3 tabs, the first contains two EditText that receive a URL and send them to the Webview of the second and third tab respectively at the push of a button, or at least that should do, until now I have achieved Send the information from the first tab to the second using an Interface. But I can not manage to send the information of the second EditText to the third tab. Thank you very much for your help.
Here is the code of my MainActivity
public class MainActivity extends AppCompatActivity implements Homefragment.SendMessage1{
TabLayout tabs;
ViewPager pages;
WebAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pages = findViewById(R.id.viewPager);
viewPagerAdapter = new WebAdapter(getSupportFragmentManager());
pages.setAdapter(viewPagerAdapter);
tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(pages);
}
@Override
public void sendData1(String message1) {
String tag1 = "android:switcher:" + R.id.viewPager + ":" + 1;
FragmentOne f1 = (FragmentOne) getSupportFragmentManager().findFragmentByTag(tag1);
f1.displayReceivedData(message1);
}
}
and here the fragment of the first tab.
public class Homefragment extends Fragment {
SendMessage1 sendMessage1;
SendMessage2 sendMessage2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.home_fragment, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnPassData = view.findViewById(R.id.btnPassData);
final EditText inData1 = view.findViewById(R.id.inMessage1);
final EditText inData2 = view.findViewById(R.id.inMessage2);
btnPassData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage1.sendData1(inData1.getText().toString().trim());
sendMessage2.sendData2(inData2.getText().toString().trim());
}
});
}
interface SendMessage1 {
void sendData1(String message1);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
sendMessage1 = (SendMessage1) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}