Skip to content

Instantly share code, notes, and snippets.

@rgdevment
Last active July 1, 2020 16:11
Show Gist options
  • Save rgdevment/466c349387319e4aa9dc2d311b70c808 to your computer and use it in GitHub Desktop.
Save rgdevment/466c349387319e4aa9dc2d311b70c808 to your computer and use it in GitHub Desktop.
[Fragment to Activity] enviar datos cuando el fragmento esta visible para el usuario por medio de interfaz a una actividad #Fragment #Java #Android
public class EnlacesActivity extends FragmentActivity implements InterfacesClass.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enlace);
}
@Override
public void onFragmentCreated(HashMap<String, String> data) {
Log.d("onFragmentCreated", String.valueOf(data));
}
}
public class InterfacesClass {
public interface OnFragmentInteractionListener {
void onFragmentCreated (HashMap<String, String> data);
}
}
public class PageOne extends Fragment implements InterfacesClass.FragmentLifecycle {
private InterfacesClass.OnFragmentInteractionListener listener;
@Override
public void onAttach(Context context) {
try {
listener = (InterfacesClass.OnFragmentInteractionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnFragmentInteractionListener");
}
super.onAttach(context);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
HashMap<String, String> dbData;
dbData = new HashMap<>();
dbData.put("dato a", "dato b");
dbData.put("dato c", "dato d");
dbData.put("dato e", "dato f");
listener.onFragmentCreated(dbData);
Log.i("Fragment", "call activity: " + dbData.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page_one, container, false);
ButterKnife.bind(this, view);
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment