Android Rapid Development appbase--(4). Detailed Com.snicesoft.Application and baseactivity
In the appbase--of Android Rapid Development (1). AppBase used Com.snicesoft.Application and baseactivity in the introduction, and this article unlocks their veil.
1. Com.snicesoft.Application1) Source Analysis
PackageCom.snicesoft;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;Importandroid.app.Activity;Importandroid.graphics.drawable.Drawable;ImportAndroid.view.View;ImportCom.lidroid.xutils.BitmapUtils;ImportCom.lidroid.xutils.DbUtils;ImportCom.lidroid.xutils.HttpUtils;ImportCom.lidroid.xutils.bitmap.BitmapDisplayConfig;ImportCom.snicesoft.avlib.AVLib;ImportCOM.SNICESOFT.AVLIB.AVLIB.LOADIMG;ImportCom.snicesoft.http.APIs;ImportCom.snicesoft.http.Charset;ImportCom.snicesoft.http.HttpReq;/** * Program application, as Application management configuration * * @author zhe * */ Public class application extends android. app. Application { PrivateHttputils httputils;PrivateBitmaputils bitmaputils;Privatehashmap<string, dbutils> dbhashmap =NewHashmap<string, dbutils> ();PrivateHttpreq Httpreq; PublicHttpreqHttpreq() {if(Httpreq = =NULL) Httpreq =NewHttpreq (httputils);returnHttpreq; } PublicHttputilsHu() {returnHttputils; } PublicBitmaputilsBU() {returnBitmaputils; } PublicDbutilsdu(String dbName) {if(Dbhashmap.get (dbName) = =NULL) Dbhashmap.put (DbName, Dbutils.create (Getbasecontext (), dbName));returnDbhashmap.get (DbName); }@Override Public void onCreate() {httputils =NewHttputils (); Httputils.configtimeout (APIs.Base.TIME_OUT); Httputils.configrequestthreadpoolsize (Ten); Httputils.configresponsetextcharset (Charset.utf_8); Bitmaputils =NewBitmaputils (Getbasecontext ()); Avlib.setloadimg (NewLoadimg () {@SuppressWarnings("Deprecation")@Override Public void loadimg(View V,intLoadingintFail, String uri) {bitmapdisplayconfig config =NewBitmapdisplayconfig ();if(Loading! =0) {drawable drawable = getresources (). getdrawable (loading); Config.setloadingdrawable (drawable); }if(Fail! =0) {drawable drawable = getresources (). getdrawable (fail); Config.setloadfaileddrawable (drawable); } bitmaputils.display (V, Uri, config); } }); }Privatelist<activity> activities =NewArraylist<activity> (); Public void addactivity(Activity activity) {Activities.add (activity); } Public void removeactivity(Activity activity) {if(Activities.contains (activity)) {Activities.remove (activity); } } Public void finishactivity(class<?> activity) { for(Activity act:activities) {if(Act.getclass () = = activity) {act.finish (); Break; } } } Public void Exitapp() { for(Activity activity:activities) {activity.finish (); } android.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (0); }}
- List and related methods are the methods used to manage activity in a common app
- Exitapp () is the real way to quit the app, only to have all of the activity completed and the process of killing the app to actually quit an app.
- Httpreq, Bitmaputils, Dbutils, httputils all configured at the time of the application, and provide the corresponding method to obtain, the purpose is to allow the entire app to keep only one object, waste of resources, Any location where the application can be obtained can be manipulated with the corresponding module unique object.
- Hu (): Return Httputils Object
- BU (): Returns the Bitmaputils object
- Httpreq (): Returns the Httpreq object
- Du (String dbName): Returns the Dbutils object based on the database name, providing multi-data support.
2) How to expand
- You can inherit com.snicesoft.Application to implement your own application (recommended)
- can also completely imitate com.snicesoft.Application own realization application
Com.snicesoft.Application in the earliest time used the static method, because the static reference to the object is not properly controlled, will cause a lot of problems, and then all changed.
2. BaseActivity1) Source Analysis
PackageCom.snicesoft.base;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportCom.snicesoft.Application;ImportCom.snicesoft.avlib.base.AvFragment;Importcom.snicesoft.avlib.base.AvFragmentActivity;ImportCom.snicesoft.avlib.rule.IData;ImportCom.snicesoft.avlib.rule.IHolder;ImportCom.snicesoft.util.FragmentUtil; Public Abstract class baseactivity<H extends iholder, D extends IData> extendsavfragmentactivity<H, D> { PublicApplicationGetapp() {return(application) getapplication (); }@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Getapp (). Addactivity ( This); }protectedavfragment<?,?,? > curfragment; Public void openfragment(intID, avfragment<?,?,? > Fragment) {if(Curfragment! =NULL&& curfragment = = fragment)return; Fragmentutil.openfragment (ID, fragment, Getsupportfragmentmanager ()); Curfragment = fragment; } Public void replacefragment(intID, avfragment<?,?,? > Fragment,BooleanBackstack) {fragmentutil.replacefragment (ID, fragment, Getsupportfragmentmanager (), backstack); Curfragment = fragment; }@Override Public void OnClick(View v) {Super. OnClick (v);if(Curfragment! =NULL) Curfragment.onclick (v); }@Override protected void OnDestroy() {Super. OnDestroy (); Getapp (). Removeactivity ( This); }}
- Baseactivity avfragmentactivity in expansion sub-avlib
2) How to expand
- Inherit baseactivity rewrite your baseactivity and add your own app's generic business (recommended)
- Inheriting the underlying activity in Avlib
Android Rapid Development appbase--(4). Detailed Com.snicesoft.Application and baseactivity