Android MVP + generics enable friendly VP interaction and activity potential memory leak optimization

Source: Internet
Author: User

 Android MVP has been a long time, in the project I also more or less use some, have to say code use this mode, the regulation is really clear a lot, the whole process seems to have a bit of a sense of each other (another Java object-oriented approach).

But here is my use of the process encountered a little bit of optimization, directly into the code to say it!!!

1 Importjava.lang.ref.Reference;2 Importjava.lang.ref.WeakReference;3 4  Public classPresenter <T>{5 6     PrivateReference<t> mreference =NULL;7     8      Public voidOnattach (T view) {9Mreference =NewWeakreference<t>(view);Ten     }; One      A      Public BooleanIsattach () { -         return NULL! = Mreference &&NULL!=mreference.get (); -     } the      -      Public voidOndettach () { -         if(NULL!=mreference) { - mreference.clear (); +Mreference =NULL; -         } +     }; A}

  The above code I briefly say here, the definition of generics is to use for the activity and its sub-class declaration (why, because presenter is specifically for view work, his definition must depend on the view function to complete.), Here you can see that I am simply using a weak reference to the argument of this view of the object reference, here I mainly consider in case the activity does not go OnDestroy method, but the program has exited, the reality of this possibility is very small, but there is really exist, Weak references are mostly added for this purpose.

and see baseactivity together.

1  Public Abstract classBaseactivity<v, TextendsPresenter<v>>extendsactivity{2 3     protectedT Mpresenter;4     5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8Mpresenter =Createpresener ();9Mpresenter.onattach ((V) This);Ten     } One  A      - @Override -     protected voidOnDestroy () { the         //TODO auto-generated Method Stub -         Super. OnDestroy (); - Mpresenter.ondettach (); -     } +      -     protected AbstractT Createpresener (); +}

As you can see here, because it's baseactivity, I want him to do the job of creating presenter objects when Activty is created, and telling presenter that he has been created and injecting itself into presenter, To facilitate the completion of P to V callback work. T extent presenter is a subclass object that is presenter to ensure that Createprenter returns. 8 lines are not empty. The purpose of direct use is to better use the idea of MVP, so the abstract method here is not allowed to return null, this is my personal design, but if you want to return null, you can sit down further.

Simple VP interaction above, I was defined according to individual needs (Attach, Dettach, Isattach).

   

Android MVP + generics enable friendly VP interaction and activity potential memory leak optimization

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.