Android Tutorial #2 - Using Singleton Object In Android Applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this tutorial we will talk about singleton an Android application singleton is an object created only one time it will not be destroyed until your entire application process is destroyed it is accessible by the entire application I will show you where to use it and how to create it fortunately I made a tutorial on how to use recyclerview for discipling list of users you can find the link in the video description I can use this code to show you where we can use singleton users list is owned by this activity later on will create two more activities one activity for adding new user I'll call it add user and one for Discipline details of the selected user from the list I'll call it user details both of them need access to the users list so that's mean users list must be generic not for a specific component as a rule of thumb anything needs to be accessed globally added to the singleton object plus learn how to create singleton object in the project window right-click on the applications package new Java class call it users base click OK singleton object needs to be instantiated or created only one time so its constructor must be private no one can make an instance of this class only this class itself create public static method or returning singleton object singleton object needs to be saved as a private static instantiate singleton object this instantiation is done only if singleton object is not being created so you have to check if singleton object is equal to null if yes then that's me the singleton object is not being created and needs to be created finally return it any component needs access to singleton object must make a call to its get method so plus say users list as a member variable of singleton object once the singleton object is instantiated for the first time instantiate list object and populated with 100 demo user object you create public method for returning list of type user call it get users return the users list back to the list of users activity it is not the honor of the users list anymore so just access it from the singleton object you users list is already instantiated unpopulated in the singleton object just remove this code we donated run the application you so singleton object is now the honor of the users list if you want to add delete update or read users data you ask singleton object for it that's all I hope it was easy to follow and helpful thanks for watching
Info
Channel: Safaa Al-Hayali
Views: 8,119
Rating: 4.961165 out of 5
Keywords: android, android app, android development, building android app, android tutorial, how to, singleton object, android studio, build app in android studio, Safaa Al-Hayali, saf3al2a
Id: 5dq9m0OiS_E
Channel Id: undefined
Length: 4min 33sec (273 seconds)
Published: Tue Jun 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.