Android handler to prevent memory overflow

Source: Internet
Author: User

Time: April 20, 2016 14:21:49
  
 
  1. static class MyHandler extends Handler {
  2. private WeakReference<Context> weakReference;
  3. public MyHandler(Context context) {
  4. weakReference = new WeakReference<>(context);
  5. }
  6. @Override
  7. public void handleMessage(Message msg) {
  8. XXActivity activity = (XXActivity) weakReference.get();
  9. if (activity != null) {
  10. activity.textView.setText(result);
  11. }
  12. }
  13. }
Note: In the highlighted section, you need to convert the context obtained from Weakreference.get () to the activity subclass you want to get the appropriate control for the current activity. If you simply convert to activity, you cannot get the controls under the activity and manipulate them.

From for notes (Wiz)

Android handler to prevent memory overflow

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.