MVP Architecture in Android

Source: Internet
Author: User

The MVC architecture of Android applications, activity often acts as a dual role in view and control, resulting in strong code coupling. How to decouple the view and control, you can use the MVP architecture (Model, Control, Prestener) to completely separate the activity's view and control, do not talk nonsense directly on the code bar!

Github:https://github.com/allin1579/allin-android

In the case of activity, the principle of fragment is the same:

View: We treat activity as a simple view

 Packagecom.allin.android.activity;ImportCom.allin.android.presenter.BasePrestener;Importandroid.app.Activity;ImportAndroid.os.Bundle;/*** Base class for activity in MVP architecture, primarily used to correlate with prestener and manage lifecycle *@authorAllin * 2015.11.24 *@param<V> baseactivity Subclass The View interface to inherit, which declares the various view operation logic of activity, V in MVP@param<P> Baseprestener Subclass, P in MVP*/ Public Abstract classBaseactivity<v, PextendsBaseprestener<v>>extendsActivity {protectedP Mprestener;//holds a reference to a Prestener object that is used to invoke the data logic in Prestener@SuppressWarnings ("Unchecked") @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Mprestener=Createprestener (); Mprestener.attachview ((V) This); } @Overrideprotected voidOnDestroy () {Super. OnDestroy ();    Mprestener.detachview (); }        protected AbstractP Createprestener ();//Mprestener Object deferred to sub-class production    }

Prestener: Process control used by intermediaries to handle data logic and view logic in views

 PackageCom.allin.android.presenter;Importjava.lang.ref.Reference;Importjava.lang.ref.WeakReference;/*** Prestener base class in MVP architecture, primarily used to correlate with view and manage lifecycle *@authorAllin * 2015.11.24 *@param<V> View Interface*/ Public Abstract classBaseprestener<v> {    protectedReference<v> Mviewref;//holds a reference to V to invoke the view logic in activity or fragment         Public voidAttachview (V view) {Mviewref=NewWeakreference<v> (view);//use weak references here to prevent memory leaks due to activity or fragment abnormal shutdown    }        protectedV GetView () {returnMviewref.get (); }         Public Booleanisviewattached () {returnMviewref! =NULL&& mviewref.get ()! =NULL; }         Public voidDetachview () {if(Mviewref! =NULL) {mviewref.clear (); Mviewref=NULL; }    }    }

At this point, V and C are fully decoupled.

To avoid prestener bloat, we can further separate the specific logic, taking the networking API as an example:

 PackageCom.allin.android.api;ImportJava.util.Map;/**API base class *@authorAllin * 2015.11.24*/classBaseapi {/*** HTTP GET request *@paramURL *@paramparams *@paramcallback callback the data obtained by the network to the caller*/     Public voidHttpGet (FinalString URL,Finalmap<?,? > params,FinalApicallback Callback) {        //TODO auto-generated Method Stub    }        /*** HTTP POST request *@paramURL *@paramparams *@paramcallback callback the data obtained by the network to the caller*/     Public voidHttpPost (FinalString URL,Finalmap<?,? > params,FinalApicallback Callback) {        //TODO auto-generated Method Stub    }}

API Callback Interface:

 PackageCom.allin.android.api;/*** Apicallback *@authorAllin * 2015.11.24*/ Public InterfaceApicallback {/*** Callback for successful network request *@paramresult*/<T>voidonsucceed (T result); /*** Callback for Network request failure *@parammsg*/    voidonfailed (String msg);}

MVP architecture in Android

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.