Android Basic Teaching context using detailed _android

Source: Internet
Author: User

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.

Difference relation:

Copy Code code as follows:

public class MyActivity extends activity {
public void Method () {
Mcontext = this; Since activity extends 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:

Copy Code code as follows:

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.
Implementation example:

Copy Code code as follows:

public class MyActivity extends activity {
private static drawable Sbackground;
protected void OnCreate (Bundle state) {
Super.oncreate (state);
TextView label = new TextView (this);
Label.settext ("Leaks are bad");
if (Sbackground = = null) {
Sbackground = getdrawable (R.drawable.large_bitmap);
}
Label.setbackgrounddrawable (sbackground);//drawable attached to a view
Setcontentview (label);
}
}

The program looks simple, but it's a big problem. There is a leak when the screen rotates (that is, the GC cannot destroy the activity).
We have just said that the system destroys the current activity when the screen rotates. However, when drawable is associated with the view, Drawable saves the reference of the view, that is, Sbackground holds the reference to the label, and the label holds the reference to the activity.
Since drawable cannot be destroyed, neither the reference nor the indirect reference can be destroyed, so that the system has no way to destroy the current activity, resulting in a memory leak. The GC is powerless for this type of memory leak.

The way to avoid this memory leak is to avoid the lifecycle of any object in the activity and to avoid the activity being destroyed as a result of the object's reference to the activity.
We can use the application context.
The application context accompanies the life of application, irrespective of the lifecycle of the activity.
The application context can be obtained by context.getapplicationcontext () or the Activity.getapplicationcontext () method.

To avoid a context-related memory leak, keep the following points in mind:
1. Do not allow long life-cycle objects to reference the activity context, that is, to ensure that the object referencing the activity is the same as the life cycle of the activity itself
2. For long life cycle objects, you can use the application context
3. Avoid non-static internal classes, use static classes as much as possible, avoid life cycle problems, and note the lifecycle changes caused by internal classes to external object references

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.