What is the Context? - Android Basics 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and welcome back to a new video and a new part of Android basics in which I will talk about a concept called context if you are a native and a developer then you will really not get around this topic of context which in and of itself is quite abstract but in this video I will break it down so you can easily understand it so a context in Android Studio is nothing else than just an instance of a class so you can see we can create some kind of variable here in my context and that can have the type of context which comes from Android content and you will really need to deal with these context objects all the time in Android development so what is this object used for in and of itself this context object can be referred to some kind of bridge between your Android app and the rest of the Android operating system and it's called context because it provides context for your application to operate within the larger scope of the whole Android operating system and well when it comes to exync use cases of these contacts objects then it's pretty much always needed whenever your app needs to communicate talk to other system components or other apps so for example if you need to get access to resources which I will talk about in the next video so if you have some static images for example you want to show if you have unlocalized strings you want to display them to get these you need access to the context or another example would be databases or preferences if you want to use database on Android or some preferences then that needs to save something on the actual file system of your Android device and since that file system is kind of an Android component and part of the Android system you need the context in order to be able to write to this file system but also for example if you want to launch a different activity either from your app or from another app for example if your app says hey I have a text file here um please open that in an app that supports opening text files then to open that other activity which is able to read text files that would need the context so in the end it's really nothing else than a middleman between your app and other components because also if you were to create some kind of empty class here my class which does something which contains some kind of logic then this class itself has zero connections to the Android ecosystem because that's just a pure kotlin class so if you would want want to actually write some data through a file for example inside of this class then by default it wouldn't know how to do that because it wouldn't know where to get all this file information from where to save it these kinds of things so in that case this class would need access to a so-called context object and then suddenly it is connected to the Android side of the app you can say and it can then use this context instance to do all these things I mentioned to write of files to open database connections to launch other apps and these kinds of things a lot more you really need this context very often in Android and now while this might still sound a little bit abstract what a context now really is let's break it down because context in the end is nothing else than a super class because if we take a look at this little diagram here and then take a look here at this context object then we can see one of its subclasses is a context wrapper we don't need to take further look at that but below that we have activities services and our application itself I will go into services in a separate video here in this Android Basics playlist later we don't want to focus on that yet but let's focus on activity and application so what this diagram basically shows is that activity in application are subclasses of context and we can also verify that in our app itself because if we create my context object again we specified as such then we can set this to this so this in this case refers to this instance of the main activity and since each activity is also a context since that's a subclass of context we can also use activities as the context and the same is the case for application and that was something I didn't talk about yet but we can also have an application class for example my application if we create that in our root package um whoops my application which would then inherit from application which comes from Android and in here this application also has a normal life cycle just as activities for example in oncreate here we could also have my context which is a context and we set it to this so this application class is also a subclass of the context superclass and the same is true for services which I will get to in a later video as I said but what is now the difference between an application context or context that comes from or refers to an application class and an activity context well each context has a specific Lifetime and for example this activity context is active as long as the activity is active and as you already know each activity has a life cycle and when that activity is destroyed all its components all its resources it uses are freed up and that includes the context so when an activity is destroyed its context is also destroyed and the same is true for applications so when the application is destroyed which also has an on Destroy function here or actually not on terminet I think it's called there's at least some kind of way to react to when the app is destroyed then this application-wide context would also be destroyed so this context effectively has a longer lifetime than the context of our activity because when the activity is destroyed for example due to configuration change like a screen rotation as I talked about in the last video then the contact reference would be destroyed with applications that wouldn't happen because this context is really active as long as your app is alive only when your app is killed for some reason maybe because the user killed it and swiped it away in the recently used apps tab or if the system just needs the memories and decides to kill your app only then this application-wide context is also destroyed and now you might wonder how we can benefit from these different context lifetimes and if we actually benefit from that in any way or if we should care about that yes you should definitely care about that because these contacts can sometimes lead to memory leaks um so that means that your app uses some amount of memory that it just requests to save some kind of objects or variables in but then under certain circumstance it can happen that it never frees that up so that it never says hey this memory cannot be used by other apps again and this can often happen with activity context so let's say we have a view model let's just quickly create a view model my view model which is an Android view model here and if we don't go to our main activity and we create an instance of this view model with the initializer I showed you in the last video about view models this one here my view model and for that I go inside of this view model and we store a context reference here so private VAR context for example like this make it nullable initially and we have a function oh let's make it super easy we just make that a public variable so imagine you just want to save this context reference in the view model to later on maybe connect to database in here or to do some other things that are yeah kind of connected to the Android ecosystem and you then go to your activity and you say viewmodel oops not that one viewmodel dot context and you set it to this so to the current activity context then what happened is you created a memory leak because this view model's lifecycle will outlive the lifecycle of the activity which is what I explained in the last video but since it still accesses resources of the activity which should normally be destroyed after a screen rotation for example they aren't destroyed simply because of the garbage collector will still see okay this context object is still needed in this view model class so I better not collect it and better not free up the memory but since the activity is destroyed which this context was connected to it can be used anymore yet it still occupies memory so that is why it's always recommended to not store activity context somewhere outside of an activity at least not in components which have a different lifetime as the activity itself and as you can see we also get a warning here so Android Studio is smart enough this field leaks a context object so let's better remove this remodel class because this is not a thing you want to do they need anyway remove move our view model here and remove it here with the application context however this can't happen because the application context is active as long as our app is active and since there is no life cycle in our app that is longer than our application lifecycle the application context can't cause these leaks and you can also retrieve this application context from a normal context object in an activity so this would refer to the activity and then we can also refer to the application context as you can see yet there are still some other reasons why I would recommend to not use any context object at all in view models these are more advanced and are related to testing nothing for this Android Basics playlist but what I'm trying to say is with this application context you can't cause any leaks but now you might wonder why shouldn't I just then always use the application context why do we need these activity contexts at all and that was a good question and the answer is there are simply certain scenarios where you have to use an activity context because you need information about the activity itself one example would be that you want to request some certain permissions on Android or whenever you want to do something that potentially accesses private user data or so or just some kind of sensitive action like um using the phone's camera using the phone's microphone then you need to request the permission from the user to be able to use that and in Android we do that with or something called activity compat and then we have request permissioned and here for this function for example we actively need to pass an activity context and not a normal context so the application context won't work here and the reason is simple what this function will do is it will show a system dialog so it will overlay a transparent activity that comes from the Android system but this activity needs information about your current activity because it will be shown on top of it and of course this is something that is obviously connected to the UI of your app if you want to see this permission dialog the application context on the other side is not connected to your app's UI because that just refers to the app itself and your app could also be in the background if if you access your application context so I hope this got clear here in the next video where we will be talking about resources you will see in an actual example of how we use this context to retrieve some resources we insert here in Android Studio such as images strings colors these kinds of things and throughout this playlist here you will also see other use cases of contacts and when it makes sense or when we have to actually pass such a context so if you like these basic concept explanations then definitely subscribe to this channel because there will be many more of such Android basic videos and also other Android videos so definitely subscribe thanks for watching and I'll wish you an amazing rest of your week see you in the next video bye bye foreign
Info
Channel: Philipp Lackner
Views: 51,055
Rating: undefined out of 5
Keywords:
Id: YdnM2ZvrIFM
Channel Id: undefined
Length: 11min 22sec (682 seconds)
Published: Wed Jun 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.