How to use MVP and the official MVP architecture for more efficient analysis

Source: Internet
Author: User
Tags abstract

Reprint please indicate the source:
http://blog.csdn.net/dantestones/article/details/51445208

In the readme of the Android MVP architecture, I briefly introduced the MVP and how to write the MVP. I also put the MVP into the project, in fact, MVP does not have a fixed wording, the right to understand the idea of architecture, can have their own unique MVP writing. Git also has a lot of examples, such as Google's Android-architecture,simple's Android source code design pattern Analysis and actual combat also has the MVP discussion. Here is a reference to simple brother made a universal version of the MVP, and Google's MVP to do a bit of their own analysis. about memory leaks caused by presenter always holding activity objects

As long as we've used the MVP issue, it's probably a lot of people know. When writing the MVP, presenter will hold the view, if the presenter has the background asynchronous long time action, such as the network request, then if returns exits the activity, the background asynchronous action does not stop immediately, here will have the memory leak the hidden danger, Therefore, a method of destroying the view is added to the presenter. Now make a change in the previous project

Add Mvpview Null method in presenter public
void OnDestroy () {    
    mvpview = null;
}

When exiting, destroy the activity
@Override
protected void OnDestroy () {    
    Mvppresenter.ondestroy ();    
    Super.ondestroy ();
}
1 2 3 4 5 6 7 8 9 10 11

A similar life cycle method is added to the presenter to cancel the holding activity when exiting the activity. However, after the destruction of the need to think about a bit, the background delay operation returned, this time the view is destroyed, if the next call to the view method will throw a null pointer exception. Therefore, in the background of the delay operation needs to take into account the possibility of generating null pointers, especially network requests. Basepresenter

If every activity needs to be bound and reconciled, it's too much trouble, and now I want to have a generic presenter to add view bindings and destroy.

Public abstract class Basepresenter<t> {public   

     T MView;    

     public void Attach (T mView) {       
         this.mview = MView;    
     }    

     public void Dettach () {        
         mView = null;    
     }
}
1 2 3 4 5 6 7 8 9 10 11 12

Because the passed-in view cannot be qualified, generics are used instead of the passed-in object. With this generic presenter, I can simplify the original mvppresenter to look like this.

public class Newmvppresenter extends basepresenter<newmvpview> {private requestbiz requestbiz;    

    Private Handler Mhandler;       
        Public Newmvppresenter () {requestbiz = new requestbiziml ();    
   Mhandler = new Handler (Looper.getmainlooper ());                        
             } public void Onresume () {Requestbiz.requestfordata (new Onrequestlistener () {                
                 @Override public void onsuccess (final list<string> data) {                   
                    Mhandler.post (New Runnable () {@Override                        
                        public void Run () {mview.hideloading ();                    
                   Mview.setlistitem (data);            
            }               
               });            
       } @Override      public void onfailed () {mview.showmessage ("request Failed");    
      }        
        });    
    } public void Onitemclick (int position) {Mview.showmessage ("clicked on item" +position); }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 Baseview

There are many similar UI methods in the UI methods that the interface needs to provide, and you can extract them into a common parent class interface. For example, to extract the display loading interface and hide the loading interface, the other view layer interface can directly inherit the Baseview interface, do not have to repeat the display and hide the loading interface method.

Public interface Baseview {    
    void showloading ();   
    void Hideloading ();
}
1 2 3 4 basemvpactivity

Presenter binding to activity and view bindings and binding operations are done by every activity, and here I would like to have a parent class to accomplish this unified operation.

Public abstract class Basemvpactivity<v,t extends Basepresenter<v>> extends appcompatactivity {public   

    T presenter;    

     @Override   
     protected void onCreate (Bundle savedinstancestate) {        
         super.oncreate (savedinstancestate);        
         Presenter = Initpresenter ();   
     }    

     @Override    
     protected void Onresume () {        
          super.onresume ();        
          Presenter.attach ((V) this);    
     }    

     @Override    
     protected void OnDestroy () {       
        presenter.dettach ();        
        Super.ondestroy ();    
     }   

     Instantiate presenter public
     abstract T initpresenter ();

}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Also use generics to extract common logic, presenter initialization, and view binding and unbind operations are extracted into the parent class activity. A initpresenter () was provided externally; method is used to initialize the presenter, if you want to create different parameters of the constructor can be arbitrary to create. A more general example

Using the base parent class above, the previous example is optimized to write a more useful example. Newmvpview inherits the Baseview interface, adding its own initialize ListView and Toast Information method

Public interface Newmvpview extends Baseview {    
        void Setlistitem (list<string> data);    
        void ShowMessage (String message);
}
1 2 3 4 Newmvppresenter inherits the Basepresenter class, increasing the network request and handling the Click event method
public class Newmvppresenter extends basepresenter<newmvpview> {private requestbiz requestbiz;    

        Private Handler Mhandler;       
           Public Newmvppresenter () {requestbiz = new requestbiziml ();    
        Mhandler = new Handler (Looper.getmainlooper ());            
                } public void Onresume () {Requestbiz.requestfordata (new Onrequestlistener () {                
                   @Override public void onsuccess (final list<string> data) {                    
                       Mhandler.post (New Runnable () {@Override                        
                           public void Run () {mview.hideloading ();                    
                        Mview.setlistitem (data);            
                 }                
                    });} @Override public void onfailed () {            
                 Mview.showmessage ("request Failed");    
       }        
            });     
        } public void Onitemclick (int position) {Mview.showmessage ("clicked on item" +position); }
}
1 2 3 4 5 6 7 8 9-ten-all-in-one-all-in-one
public class Newmvpactivity extends basemvpactivity<newmvpview,newmvppresenter> implements Newmvpview,    
        adapterview.onitemclicklistener{ListView Mvplistview;    

        ProgressBar PB; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinsta        
              Ncestate);        
              Setcontentview (R.LAYOUT.ACTIVITY_MVP);                 
              Mvplistview = (ListView) Findviewbyid (R.id.mvp_listview);       
              Mvplistview.setonitemclicklistener (this);    
       PB = (ProgressBar) Findviewbyid (r.id.mvp_loading);        
             } @Override protected void Onresume () {super.onresume ();    
        Presenter.onresume (); } @Override Public Newmvppresenter Initpresenter () {return new Newmvppresenter    
       (); } @Override public void Onitemclick (AdApterview<?> Parent, view view, int position, long id) {Presenter.oni 

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.