Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Context in Android Application

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 321
    Comment on it

    Context is the current state of any object/application that represents several environment data. Context assist the present activity to communicate with the outer environments like the databases, class loaders, files to the environment. It is the handle to the system that offers services like obtaining access to preferences and databases, resolving resources and so on. It allows the developer to access some specific resource of your application as resource and class relevant information regarding application environment. It exists everywhere in android programming so it's an important thing to understand, so we must know to use it correctly.

    Wrong use of Context can cause to lead memory leak in your application.

     

    There are Two Types of Context in Android Development, these are:

     

    1. Application Context

     

    Application context can be used as singleton object and can be accessed via getApplicationContext().


    This context is tied to the lifecycle of an application and assist you to access the globally local database, share prefer object and other variables and data which are using in the context of the whole application. For example, If you have to create a singleton object for your application and that object needs a context, always pass the application context. If in that case, you will pass particular activity context then it will lead to a memory leak problem. Then, in the case of initializing library, you have to pass application context, not the activity context.

     

       

         2. Activity Context

     

    Activity Context is available only in an Activity and uses the lifecycle of only that activity. You must use this context in that case only where you are passing that context in activity scope. For example: If you have to create an instance which is attached only to an activity, you can use the activity context.

     

    Hope the information was helpful. If you like the blog, please share it on the social media channels like Facebook, Twitter, etc.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: