‫Welcome back.
‫In this video, let's move to the main activity and test our application.
‫For that, I'll create some variables and initialize some widgets.
‫So private array list of movie and I name it as movies.
‫I'll press enter to import the movie class and I'll press enter to import the array list class.
‫I'll create a private recyclerview private movie adapter and I'll name it as movie adapter, private
‫swipe refresh layout and the data binding activity binding binding and the ViewModel main activity.
‫ViewModel ViewModel.
‫Let's initialize them here in the Oncreate method binding equal to data binding util dot set Contentview
‫to this as a first parameter and our dot layout dot activity main the second parameter.
‫Now the ViewModel equal to new ViewModel provider.
‫I'm initializing it passing this at the first parameter which is the owner and get which is the main
‫activity.
‫ViewModel dot class.
‫This code provides an instance of the main activity.
‫ViewModel using the ViewModel provider in the Android activity.
‫The ViewModel provider is a class provided by the Android architecture components for managing ViewModel
‫instances.
‫It's used to create and retrieve ViewModel instances associated with a specific lifecycle scope such
‫as an activity or fragment.
‫And this, in this constructor of the ViewModel provider refers to the current Android component and
‫typically an Android activity or fragment.
‫And this refers to the main activity activity that is creating or retrieving the ViewModel instance.
‫The view model's lifecycle scope is tied to its component, so it's tied to the main activity lifecycle.
‫Get method.
‫This part of the code retrieves an instance of the main activity ViewModel class.
‫It tells the ViewModel provider to either create a new instance of the ViewModel if it doesn't exist
‫for the current lifecycle, scope or return an existing one.
‫If it does, if a ViewModel with a specified class already exists for the current scope, it will be
‫returned ensuring that you have a single instance of the main activity ViewModel associated with your
‫activity if it doesn't exist and new instance of the main activity, ViewModel will be created and associated
‫with the current scope.
‫Then I'll create a method called pop.
‫Get popular movies here in the main activity under the Oncreate method.
‫And I'll start with the ViewModel Dot.
‫Get all movies dot observe method this and the new observer with a list of movies.
‫This method will use the ViewModel to observe changes in a list of movies that are fetched using the
‫get all movies method from the main activity.
‫ViewModel So this is apart from the ViewModel Observe method.
‫This line set up an observer for the live data.
‫So this method is used to observe changes in the live data.
‫The first argument this is the lifecycle owner to which the observer should be bound in Android activities
‫and fragments are typical lifecycle owners, and this refers to the main activity, which is an activity
‫and the owner of this lifecycle.
‫Then you observer and list of movie.
‫This creates an anonymous inner class that implements the observer interface for the list of movies.
‫The unchanged method inside the Observer will be called whenever the list of movies changed.
‫Unchanged method.
‫This is a callback method that gets executed when the live data containing the list of movies changes.
‫The movies parameter contains the updated list of the movies inside the unchanged method.
‫You can write code to handle changes in the list of the movies.
‫For that, I need to use this updated list to update my array list so movies equal to movies and don't
‫use the same naming.
‫So I'll change this to movies from live data, copy it and paste it here and I need to cast it as an
‫array list of movies.
‫Then I need to call a method called Show on all display movies in Recyclerview Alt+ enter to create
‫the method in the main activity.
‫So I created here.