From 1d867e50a3d6e693f9f7a0ce33329def804e7647 Mon Sep 17 00:00:00 2001 From: bhavya2921 <78963048+bhavya2921@users.noreply.github.com> Date: Wed, 23 Jun 2021 20:02:16 +0530 Subject: [PATCH] Session Report - 05 --- Android Development/Session 5.md | 92 ++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Android Development/Session 5.md diff --git a/Android Development/Session 5.md b/Android Development/Session 5.md new file mode 100644 index 0000000..9effd67 --- /dev/null +++ b/Android Development/Session 5.md @@ -0,0 +1,92 @@ +# Session 5 + +_Topic:_ View Model and Live Data + +_Conducted on:_ 21-06-2021 20:30 + +## Agenda + +A Session on implementation of View Model and Live Data + +## Summary + +Repo For reference -- https://github.com/bhavesh3005sharma/android-mvvm-architecture + +A class on implementing View Model and Live Data to store and access Data efficiently. + +### Important concepts discussed : + +#### View Model Class + + ViewModel helper is responsible for preparing data for the UI. ViewModel objects are automatically retained during configuration changes so that data they hold is immediately available to the next activity or fragment instance without any loss. + +#### Implementing View Model + +##### Code for Model Class + + public class MyViewModel extends ViewModel { + + // Any variable private or public are declared here. + + private void any_funtion() { + + } + } + +##### Accesing View Model elements from an activity + + public class MyActivity extends AppCompatActivity { + public void onCreate(Bundle savedInstanceState) { + + MyViewModel model = new ViewModelProvider(MyActivity.this).get(MyViewModel.class); + + // To access variable/funtion. + model.{Variable or Funtion of Model class}; + } + +} + +#### Live Data + +Live Data is an observable Data Holder Class +It is not Mutable + +- Declaration :- + public final LiveData liveData = new LiveData<>(); + +_To Observe the data in liveData We use observe funtion_ + +#### Mutablelive Data + +Mutablelive Data is an sub class of Live Data. +It is Mutable (Can be Modified or Changed). + +### Important Notes:- + +- Do not Reinitialize the Arraylist (Any List) , Always use clear() and add() funtions. +- Do not set adapter again and again use notifyDataSetChanged() to notify about changed data in list. + +### Resources provided : + +Videos: + +- https://www.youtube.com/watch?v=i22jxmIh_EE + +Articles: + +- Viewmodel :- https://developer.android.com/topic/libraries/architecture/viewmodel + +- LiveData :- https://developer.android.com/reference/android/arch/lifecycle/LiveData + +- MutableLiveData :- https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData + +--Reference Repo for Live Data :- +https://github.com/trulymittal/viewModel_LiveData/tree/starter_project + +# Credits + +_Conducted by_: Bhavesh Sharma , Pratham Pahariya + +_Report compiled by_: Bhavya Agrawal + +_Attendees_: Abdullah, Anisha Dutta, Ansh Tandon, Bhavya Agarwal, Ganta Nikhil, Garvit Dua, Tarun Shrivastava