In the encapsulation of the Activity base class, what do I do? activity base class Encapsulation
In development practice, many codes of different activities are redundant. Therefore, it is necessary to extract this part, encapsulate a class inherited from the Activity, and name it BaseActivity.
Looking at the previously written code, at first, BaseActivity was used only for umeng statistics. The onPause and onResume methods of each Activity were rewritten, based on which the relevant data used on the page was recorded, the Code is as follows:
public class BaseActivity extends Activity {protected String Tag;protected Context mContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Tag = this.getClass().getSimpleName();mContext = this;}@Overrideprotected void onResume() {super.onResume();MobclickAgent.onResume(this);}@Overrideprotected void onPause() {super.onPause();MobclickAgent.onPause(this);}}Next, we found that in addition to this, there is still a lot of work that can be done in BaseActivity, saving a lot of manpower and material resources.
Among them, Toast is a very common simple prompt control. After learning, it was initially used as follows:
// Display ToastToast. makeText (this, "Hello", Toast. LENGTH_SHORT). show ();
Later I found it too troublesome to write data each time. It is better to encapsulate it into a tool class, which draws on the encapsulation mode of great gods:
public class T{private T(){/* cannot be instantiated */throw new UnsupportedOperationException("cannot be instantiated");}public static boolean isShow = true;public static void showShort(Context context, CharSequence message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}public static void showShort(Context context, int message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}public static void showLong(Context context, CharSequence message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_LONG).show();}public static void showLong(Context context, int message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_LONG).show();}}You can use less than one parameter each time, and the display time is slightly shorter.
After a period of time, encapsulate the display Toast method in the Activity to see if it is more elegant:
public class BaseActivity extends Activity { //...add new method protected void showShortToast(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); }}
After inheriting BaseActivity, isn't it convenient to display Toast every time?
For example, other common UI controls can also be encapsulated.
In some projects, image libraries such as UIL third-party images can also be encapsulated, which also reduces the amount of code to a certain extent, such:
Public class ImageLoaderBaseActivity extends BaseActivity {protected DisplayImageOptions options; protected ImageLoader imageLoader; protected void onCreate (Bundle savedInstanceState) {options = new DisplayImageOptions. builder (). showImageOnLoading (R. drawable. bg_pic_loading ). showImageForEmptyUri (R. drawable. ic_launcher ). showImageOnFail (R. drawable. ic_launcher ). cacheInMemory (true ). cacheOnDisc (true ). considerExifParams (false ). imageScaleType (ImageScaleType. EXACTLY_STRETCHED ). bitmapConfig (Bitmap. config. RGB_565 ). resetViewBeforeLoading (false) // sets whether to reset and reset the image before downloading. build (); // build imageLoader = ImageLoader. getInstance (); imageLoader. init (ImageLoaderConfiguration. createDefault (this ));};}
In fact, hiding the ActionBar can also be placed in BaseActivity.
In addition, there are a lot more opportunities for discussion!
New bloggers, please pat on your face.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.