Last active
July 1, 2020 16:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InterfacesClass { | |
public interface OnFragmentInteractionListener { | |
void onFragmentCreated (HashMap<String, String> data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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