Thursday, June 3, 2010

Android- Key Notes

I have collected following information while going through various blogs, android web guide and during my own coding experience. So my due regards to the authors. These are really tiny and important pieces of information. I'll update the list as I increase my collection-

  1. Is your Intent Already Handled by other Activity? PackageManager- checks whether there is any activity which listens to an Intent. When you call startActivity(), ActivityNotFoundException is thrown if there is no Activity which handles a specific Intent. So imagine, you can prompt user to download a missing app containing the required Activity.
  2. Portrait to Landscape change- onRetainNonConfigurationInstance() and get the stored object- getLastNonConfigurationInstance() Don’t store View, Drawable, Adapter etc. UI classes into the store, which might cause Leak Window. Rather store raw data e.g. collection of bitmap, text etc.
  3. Android UI-ToolKit (Views, Drawables etc.) is not thread safe. So it’s not recommended to access view classes from a worker thread. We can use AsynchTaskwhich does heavy operation (not updating UI classes) on a Worker thread and UI update on UI Thread. Scenario- “Download an image from HTTP Server and create an ImageView”. In doInBackground() download the image and create the ImageView in onPostExecute(). IMPORTANT: Make sure that you access the Android UI toolkit only in the UI thread
  4. Synchronization lock for static method or static block is the lock of class object which is different than the lock of object instances i.e. this. This seems out of track but this is a generic knowledge and important for object synchronization.
  5. Handler- "A Handler is not a message loop, it is a recipient for messages. The messages sent to it are dispatched by the message loop of the thread that created the handler"- is a very important phrase. By default it uses Main Thread i.e. UI Thread’s message queue. You should use a different threads Message queue and shouldn’t share the one which is present in UI Thread-

HandlerThread handlerThread = new HandlerThread("Easy_Widget_Task");


Handler handler = new Handler(handlerThread.getLooper(), this);

  1. AsynchQueryHandler- AsynchQueryHandler is a sub class of Handler and it does thing in a bit smart has an internal HandlerThread. So basically the queries will be handled in a separate Worker Thread's Message Queue. You need not to provide explicitly Looper of a separate HandlerThread.
  2. Avoid Leak Window- a good article on this
  3. Getting HROF Dump from Android DDMS and ADB

-Programmatically: Debug.dumpHprofData("memo_out_exception_file_name.hprof");

-Manually: Note the PID. Send SIGUSR1 signal using adb shell command, kill -10 . One .hprof file will get created under data/misc. Convert the native Android formatted HROF file into readable format for Eclipse MAT using hprof-conv tool comes with Android SDK.

1 comment:

  1. nice post has been shared.the terms used in the article are iportant to know