Compiling base-class BaseActivity for Android App Framework Design

Source: Internet
Author: User

Compiling base-class BaseActivity for Android App Framework Design

Compiling base-class BaseActivity for Android App Framework Design

Write base-class BaseActivity
-Sub-classes in OOP can share parent classes to improve code reusability.
-You need to compile the base class based on the actual project. Some may be very simple, for example, displaying "about" information. You do not need to inherit the base class for expansion because of very few business functions.

Does BaseActivity inherit Activity, FragmentActivity, or other activities? It is also based on your technical requirements, because there are many ways to solve the problem, or you do not need to write a base class to complete the work.
Our goal is to write better code, improve code reusability and scalability, especially for team collaboration, which is one of the reasons why OOP emerged.

What should I write to the base class? Common Code.

What should I do in different methods of uniform data interface lifecycle in Network Environment Monitoring

When I was just working on Spring source code, I didn't even know why there were so many interfaces and objects. With the accumulation of experience, we know the benefits of OOP.

Below we will write a basic base class, which is not complicated. Suppose our business is the company's mobile OA system. We need to manage our employees.
Abstract an employee

// Domine is a common entity class and can be used as a generic parameter later. public class Employee extends Domine {private static final long serialVersionUID = 1L; public Employee () {} public String fullName; public String mobileTel; public String email; public String empNo;} // you can add the public attribute public class Domine implements Serializable {private static final long serialVersionUID = 1L; public Domine () {} public int id; public String desc ;}

Data Interaction Model

Public class MData
  
   
Implements Serializable {private static final long serialVersionUID = 1L; public String id; public String type; public T dataList; // multiple types of data, generally a List set, such as getting a List of all employees}
  

The following is the data callback interface. We need to notify the UI to obtain data from the network.

Public interface IDataCallback
  
   
{// We pass in the common type public void onNewData (T data); public void onError (String msg, int code );}
  

The following is an independent UIHandler, which is generally written in the Activity as an internal class.

Public class UIHandler extends Handler {private IHandler handler; // callback interface. The message is sent to the registrant public UIHandler (Looper loler) {super (looper);} public UIHandler (looper loler, IHandler handler) {super (logoff); this. handler = handler;} public void setHandler (IHandler handler) {this. handler = handler;} @ Override public void handleMessage (Message msg) {super. handleMessage (msg); if (handler! = Null) {handler. handleMessage (msg); // if there is a Message, it will be passed.} public interface IHandler {public void handleMessage (Message msg );}

We have basic things. Now we can combine them into BaseActivity.

Public abstract class BaseActivity extends Activity {// you can put constants in a Class separately. public static final String ACTION_NETWORK_CHANGE = "android.net. conn. CONNECTIVITY_CHANGE "; public static final String ACTION_PUSH_DATA =" fm. data. push. action "; public static final String ACTION_NEW_VERSION =" apk. update. action "; protected static UIHandler handler = new UIHandler (lohand. getMainLooper (); // data callback interface, all of which pass the Domine subclass entity protected IDataCallback
  
   
> DataCallback; public BaseActivity () {}// the design pattern of "template method" is somewhat like @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setBase (); setHandler (); initContentView (savedInstanceState);} private void setHandler () {handler. setHandler (new IHandler () {public void handleMessage (Message msg) {handler (msg); // The method submitted to the subclass for implementation if there is a Message});} // initialize the UI, setContentView and other protected abstract void initContentView (Bundle savedInstanceState); // private void setBase () {requestWindowFeature (Window. FEATURE_NO_TITLE); // example} protected void addLeftMenu (boolean enable) {// if your project has a slide bar, you can handle this method if (enable) {// is there a slide bar} else {}} // Let the subclass process the Message protected abstract void handler (Message msg); // you can switch between the portrait and landscape screens, keyboard, etc. @ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig) ;}@ Override protected void onRestoreInstanceState (Bundle savedInstanceState) {try {super. onRestoreInstanceState (savedInstanceState);} catch (Exception e) {}}@ Override protected void onResume () {super. onResume (); // you can add multiple actions to capture IntentFilter filter = new IntentFilter (); filter. addAction (ACTION_NETWORK_CHANGE); filter. addAction (ACTION_PUSH_DATA); filter. addAction (ACTION_NEW_VERSION); registerReceiver (receiver, filter); // it is also possible to send statistics, such as third-party sdks for statistics} @ Override protected void onPause () {super. onPause (); unregisterReceiver (receiver ER); // it is also possible to send statistics, such as third-party sdks for statistics} BroadcastReceiver receiver ER = new BroadcastReceiver () {@ Override public void onReceive (Context context, Intent intent) {// process various cases String action = intent. getAction (); if (ACTION_NETWORK_CHANGE.equals (action) {// network change // handle network problems} else if (ACTION_PUSH_DATA.equals (action )) {// new data Bundle B = intent may exist. getExtras (); MData
   
    
Mdata = (MData
    
     
) B. get ("data"); if (dataCallback! = Null) {// data notification dataCallback. onNewData (mdata) ;}} else if (ACTION_NEW_VERSION.equals (action) {// The new version may be found. // The VersionDialog may be a dialog box Indicating whether to download the version prompt }}}; public void setDataCallback (IDataCallback
     
      
> DataCallback) {this. dataCallback = dataCallback ;}}
     
    
   
  

With the base class, write a subclass Activity.

Public class EmployeeDisplayActivity extends BaseActivity implements IDataCallback
  
   
> {// Implement the data callback interface public EmployeeDisplayActivity () {}@ Override protected void initContentView (Bundle savedInstanceState) {setContentView (R. layout. activity_main); loadData ();} private void loadData () {setDataCallback (this); // you can set the Callback function. // You can also pass this Callback to other classes for data retrieval, for example, HTTP obtains data // For example, HttClient. get (url, this) ;}@ Override public void onNewData (MData
   Data) {// update the UI data final List
   
    
List = (List
    
     
) Data. dataList; handler. post (new Runnable () {public void run () {// update UI}); // or handler. sendEmptyMessage (0); // notification Handler} @ Override public void onError (final String msg, final int code) {handler. post (new Runnable () {public void run () {// notification error message}); // or handler. sendEmptyMessage (0); // notification Handler} @ Override protected void handler (Message msg) {// we can process the data Message switch (msg. what) {case 0: break; case-1: break; default: break ;}}}
    
   
  

UML diagram

This article has compiled the basic BaseActivity code, which is obviously not perfect and requires continuous learning and improvement. If you have any questions, please refer to Thanks.

Related Article

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.