Android Context Detailed

Source: Internet
Author: User

The Android context can do a lot of things, but the main function is to load and access resources.

There are two kinds of context in Android, one is the application, the other is the context of activity, and we usually pass the activity context among various classes and methods.

Inheritance Relationship:

Difference relation:

public class MyActivity extends activity {public     
    void method () {     
       mcontext = this;    Since activity extends the context     
       Mcontext = Getapplicationcontext ();     
       Mcontext = Getbasecontext ();     
    }     
 }

This is an instance of the activity that extends the context, whose lifecycle is activity creation to destruction

Getapplicationcontext () Returns the context of the application, the lifecycle is the entire application, and the application destroys it before it destroys

Activity.this the context of the current activity, which belongs to the activity, destroys the activity and destroys it.

Getbasecontext () returns the context specified by the constructor or Setbasecontext (), with little SDK documentation and no recommended

Figuring out the lifecycle is going to make mistakes in the process of use, such as having a global data manipulation class used in context, this time using getapplicationcontext rather than activity, This ensures that the operation of the database is not related to the activity (it does not always refer to the resources of the action, preventing memory leaks)

Application Scenario:

such as the oncreate of an activity:

protected void OnCreate (Bundle state) {             
     super.oncreate (state);             
     TextView label = new TextView (this); Pass context to view Control             
     Label.settext ("Leaks are bad");             
     Setcontentview (label);     

Passing the activity context to view means that the view has a reference to the activity, which in turn refers to the resources the activity occupies: view hierachy, Resource, and so on.

This will reveal a lot of memory if there is a memory leak in the context. The leak here means that the GC has no way to reclaim the activity's memory.

The activity of leaking an entire is very easy. When the screen rotates, the system destroys the current activity, saves state information, and creates a new activity.

For example, we write an application that needs to load a large picture, and we don't want to destroy the picture every time we rotate the screen to reload it.

The simple idea of implementing this requirement is to define a static drawable so that the activity class creates and destroys it and is always saved in memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.