Droid-Fu is a general Android Application Library
The main purpose of Droid-Fu is to make Android development easier.
Droid-Fu is open-source
Droid-Fu contains many tool classes and very easy-to-use Android components, all of which are very useful for Android applications. Droid-Fu provides support in the following fields:
Lifecycle of Android applications
Background task
Http message processing
Remote Image Processing
Various custom adapters
Basic knowledge
The biggest advantage of Droid-Fu is its application lifecycle help class. If you are developing an Android Application and its main task is to run background tasks, such as capturing data from the web, you will certainly use Droid-Fu in your application.
Annoying AsyncTask
I have discussed this topic in two articles. For the third time, I finally found an elegant solution. Let's take a look at AsyncTask: first, what is AsyncTask? It is a help class for executing large sales operations. If you are developing a mobile app that needs to access the web server, you may have used it.
The basic idea is to initiate an asynchronous task. When the task thread executes the task, a beautiful progress box is displayed. When the task returns the result to the Activity, the Progress box is closed. Cool, but if you decide to rotate the screen, what can be done while asynchronous tasks are still running (rotating the current Activity on the screen will complete the entire lifecycle. Then recreate and call back the onCreate () method .)? Or when a phone call comes in and interrupts the app, Android decides to kill the app process. What should I do? Both of these actions will interrupt the activity. When you continue to use this application, the Activity will be recreated. Unfortunately, AsyncTask is still running, and a pointer is reserved to point to the old Activity, because the restarted Activity is a completely different object in the memory (and the onCreate () of the Activity will be executed (), as if Activity was started for the first time ). I'm not sure if AsyncTask will return the result to the old Activity object (if it is a weak reference, it may have been recycled), but in any case, the new Activity will not receive the data, because it is a new Activity instance.
Now, people can say: Okay, let's do the same job again and resend the request. Yes, you can. But this is a waste. Really, it makes you feel stupid, isn't it? In addition, if you trigger a Web service request, flip the screen, and find that the request is not what you want and is flipped back, your request is sent three times parallel. Is that what you want? Probably not.
BetterAsyncTask
Fortunately, Droid-Fu provided us with a solution: BetterAsyncTask! (I am a lazy person. I cannot come up with a better name ). Its behavior is exactly the same as AsyncTask (in fact, this is an AsyncTask), but it does some extra work for you: First, it records the currently active instance. If the instance changes, return the released data to the new instance. In other words, you can dispatch your task and flip the screen back and forth at will. BetterAsyncTask will still return the task result to the current active instance. In other words, the task runs only once.
There is a problem because BetterAsyncTask cannot find out which instance is currently active, and it depends on a helper: DroidFuApplication. This class is a subclass of Application. If you want to use BetterAsyncTask, your Application derives from this class, otherwise it will not work. This is because DroidFuApplication retains the weak ing of the active instance. After a BetterAsyncTask is completed, it obtains the Activity instance through your Application.
All you need to do is to initiate a task:
Public class MyActivity extends BetterDefaultActivity {
Protected void onResume (){
Super. onResume ();
If (isLaunching () {// this is explained further down this article
MyBetterAsyncTask task = new MyBetterAsyncTask (this );
Task.exe cute (someData );
}
}
}
In addition, BetterAsyncTask automatically opens and closes the dialog box at runtime, even if you progress or trigger the progress bar of the activity. It also allows your task to throw an exception during execution and then release it to your error handling handle.
BetterActivity and BetterService
Droid-Fu provides simple but useful auxiliary base classes for activities and services. Currently, only BetterDefaultActivity, BetterListActivity, and BetterService are available. However, I plan to implement the base classes (such as MapActivity) of the Better * series Android activity ).
You will get help in the following aspects
Lifecycle help class
These methods allow you to decide what to do based on the context's lifecycle status.
IsLaunching (): true only when the Activity or Service executes onCreate for the first time
IsRestoring (): true if Activity or Service is restored after being killed by Android.
IsResuming (): true if onCreate is not executed before onResume is executed by Activity or Service.
IsApplicationBroughtToBackground (): it is sometimes necessary to distinguish whether your activity is paused by your own application activity or by a completely different application. This method can be used to obtain the truth. If another application suspends your activity, true is returned.
Dialog help class
BetterActivity provides the following help methods for you to easily display Dialog
ShowInfoDialog (): displays a warning dialog box with a notification icon.
ShowAlertDialog (): a warning dialog box with a warning icon is displayed.
ShowErrorDialog (): similar to showAlertDialog, only one parameter is added to display exception information.
ShowListDialog (): A List dialog box is displayed. When you click an entry, it calls back the handler.
All these Dialog s can be customized (e.g. message or icon ).