Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente |
lang:android:fragment [2023/08/23 11:48] – [LiveData] : ajout de sources root | lang:android:fragment [2023/08/23 12:15] (Version actuelle) – Ajout de "Binding" root |
---|
[[https://developer.android.com/guide/fragments/communicate?hl=fr#java|Communiquer avec des fragments]] {{ :lang:android:fragment:communiquer_avec_des_fragments_android_developers_22_08_2023_16_08_14_.html |Archive du 30/05/2023 le 22/08/2023}} | [[https://developer.android.com/guide/fragments/communicate?hl=fr#java|Communiquer avec des fragments]] {{ :lang:android:fragment:communiquer_avec_des_fragments_android_developers_22_08_2023_16_08_14_.html |Archive du 30/05/2023 le 22/08/2023}} |
| |
| Le respect des bonnes pratiques de communication devrait permettre de ne pas avoir besoin d'utiliser ''runOnUiThread(...)''. |
====Événements==== | ====Événements==== |
| |
| |
[[https://stackoverflow.com/questions/72634225/onbackpressed-is-deprecated-what-is-the-alternative|onBackPressed() is deprecated. What is the alternative?]] {{ :lang:android:fragment:android_-_onbackpressed_is_deprecated._what_is_the_alternative_-_stack_overflow_23_08_2023_09_36_51_.html |Archive du 27/07/2023 le 23/08/2023}} | [[https://stackoverflow.com/questions/72634225/onbackpressed-is-deprecated-what-is-the-alternative|onBackPressed() is deprecated. What is the alternative?]] {{ :lang:android:fragment:android_-_onbackpressed_is_deprecated._what_is_the_alternative_-_stack_overflow_23_08_2023_09_36_51_.html |Archive du 27/07/2023 le 23/08/2023}} |
| |
| =====Binding===== |
| |
| Il existe ''View binding'' et ''Data binding''. |
| |
| Si l'objectif est seulement de remplacer les ''findViewById'', il faut utiliser ''View binding'' car plus léger. |
| |
| Si l'objectif est également de maîtriser les données lors de la reconstruction d'un fragment, ''Data binding'' est nécessaire. |
| |
| ====View binding==== |
| |
| ====Data binding==== |
| |
| * Chargement du fragment |
| |
| <code java> |
| @Override |
| @Nullable |
| public View onCreateView(@NonNull final LayoutInflater inflater, |
| @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) { |
| super.onCreateView(inflater, container, savedInstanceState); |
| |
| binding = NameFragmentBinding.inflate(inflater, container, false); |
| binding.setLifecycleOwner(getViewLifecycleOwner()); // Uniquement nécessaire si le binding est lié à un LiveData. |
| |
| return binding.getRoot(); |
| } |
| </code> |
| |
| [[https://stackoverflow.com/questions/63336247/must-i-set-lifecycleowner-for-a-data-binding-in-android-studio)|Must I set lifecycleOwner for a data binding in Android Studio?]] {{ :lang:android:fragment:must_i_set_lifecycleowner_for_a_data_binding_in_android_studio_-_stack_overflow_23_08_2023_12_07_42_.html |Archive du 10/08/2020 le 23/08/2023}} |
| |
| [[https://developer.android.com/topic/libraries/data-binding/architecture|Bind layout views to Architecture Components]] {{ :lang:android:fragment:bind_layout_views_to_architecture_components_android_developers_23_08_2023_12_06_13_.html |Archive du 12/07/2023 le 23/08/2023}} |
| |
| * Utilisation des LiveData |
| |
| [[https://medium.com/swlh/basic-form-validation-in-android-with-live-data-and-data-binding-cb6b1d073717|Basic Form Validation in Android with Live Data and Data Binding]] {{ :lang:android:fragment:basic_form_validation_in_android_with_live_data_and_data_binding_by_brandon_wever_the_startup_medium_23_08_2023_12_12_03_.html |Archive du 07/10/2019 le 23/08/2023}} |
| |
| ====Problèmes===== |
| |
| Si une ressource ne génère pas le binding, il faut entourer la ressource par ''<layout></layout>''. |