In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use context in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Context can do many operations in Android, but the main function is to load and access resources. There are two kinds of context in android, one is application context and the other is activity context. Usually we pass activity context between various classes and methods, such as an onCreate of activity.
Java Code:
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 activity context to view means that view has a reference to activity, which in turn refers to resources owned by activity: view hierachy, resource, and so on.
In this way, if a memory leak occurs in context, a lot of memory will be leaked.
The leak here means that gc has no way to reclaim activity's memory.
Leaking an entire activity is a very easy thing.
When the screen rotates, the system destroys the current activity, saves the status information, and creates a new one.
For example, we wrote an application that needs to load a large image, and we don't want to destroy the image and reload it every time we rotate the screen. The simple idea to implement this requirement is to define a static Drawable so that the Activity class creation destroys it always in memory.
The implementation is similar to:
Java Code:
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);}}
This program looks simple, but it is very problematic. There will be a leak when the screen rotates (that is, gc cannot destroy the activity). As we just said, the system destroys the current activity when the screen rotates. But when drawable is associated with view, drawable saves the reference of view, that is, sBackground holds a reference to label, while label holds a reference to activity. Since drawable cannot be destroyed, neither its references nor indirect references can be destroyed, so there is no way for the system to destroy the current activity, resulting in a memory leak. There is nothing gc can do about this type of memory leak.
The way to avoid this memory leak is to prevent any object in the activity from having a longer life cycle than the activity, and to prevent the activity from being destroyed properly due to the object's reference to the activity. We can use application context. Application context accompanies the life of application and has nothing to do with the life cycle of activity. Application context can be obtained through the Context.getApplicationContext or Activity.getApplication method.
To avoid context-related memory leaks, keep in mind the following:
1. Do not let objects with long lifecycles refer to activity context, that is, make sure that objects referencing activity are the same as the lifecycle of activity itself.
two。 For objects with a long life cycle, you can use application context.
3. Avoid non-static inner classes, try to use static classes, avoid life cycle problems, and pay attention to life cycle changes caused by internal class references to external objects.
This is the end of the content of "how to use context in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.