Monday, November 16, 2015

Whats minimum you can do to detect activity leaks in an Android App ?

Just enable the strict mode. Assuming you are coding at midnight and still want to make sure that there are no lingering activity leaks in your app , you think its too much to force a heap dump and analyze that in MAT / JHAT , well here comes the StrictMode to your rescue.

 if (BuildConfig.DEBUG) {  
   StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
       .detectAll()  
       .penaltyLog()  
       .build()); 
   StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
       .detectAll()  
       .penaltyLog()  
       .penaltyDeathOnNetwork()  
       .build());  
 }   

It totally came to me as a surprise but yeh its true and its awesome

Stack

com.cricket.material E/StrictMode: 
class com.cricket.material.cricket.Summary.LiveScoreSummaryDetail; 
instances=2; limit=1 
com.cricket.material E/StrictMode: 
android.os.StrictMode$InstanceCountViolation:  
class com.cricket.material.cricket.Summary.LiveScoreSummaryDetail;  
instances=2; limit=1
com.cricket.material E/StrictMode: 
at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)

Explanation :

Take a look at  StrictMode
/**
2324 If this is a instance count violation, the number of instances in memory,
2325 else -1.
2326 */
2327 public long numInstances = -1;

2188 Returns an object that is used to track instances of activites.
2189 The activity should store a reference to the tracker object in one of 
     its fields.
2190 * @hide
2191 */
2192 public static Object trackActivity(Object instance) {
2193       return new InstanceTracker(instance);
2194}

Note:

Please take this opportunity to understand other useful features of StrictMode
  • NetworkOnMainThread exception

Sunday, November 15, 2015

Don't override the default constructor of a fragment.

You should not override the default empty constructors of fragments, the reason for that is framework recreates fragments accross orientation change using the deafault constructor and any initialization that you do in non default constructor will be lost

But to really experiment about what happens , try to override the constructor and cause an orientation change , you will see that most likely you will hit a null pointer exception if you have passed an object to fragment constructor and tried to access the same after orientation change

This below is an ideal way to use fragment
  /**  
    * Use this factory method to create a new instance of  
    * this fragment using the provided parameters.  
    *  
    * @param param1 Parameter 1.  
    * @return A new instance of fragment SquadFragment.  
    */  
   // TODO: Rename and change types and number of parameters  
   public static SquadFragment newInstance(String param1) {  
     SquadFragment fragment = new SquadFragment();  
     Bundle args = new Bundle();  
     args.putString(ARG_PARAM1, param1);  
     fragment.setArguments(args);  
     return fragment;  
   }  
   
   public SquadFragment() {  
     // Required empty public constructor  
   }  
   
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     if (getArguments() != null) {  
       mParam1 = getArguments().getString(ARG_PARAM1);  
     }  
     mSquadAdapter = new SquadRecyclerViewAdapter(mParam1);  
   }  

Saturday, November 14, 2015

Android Services

There are actually two ways to implement a service 
  1. When your class MyService extends from Service 
  2. When your class MyService extends from IntentService

Rule of thumb:

No matter which service you implement , lifecycle methods of the service will be called on main thread aka UI Thread and this is where i have seen too much of confusion where newbies tend to think that all the methods in the service are called on background thread .
Because of this you should not be calling any methods that would essentially be calling any methods which would block your UI thread in lifecycle methods of service 

Things are not that BAD as you think as you might start to wonder about whats the point then...
  • If you implement #1 you have to start a worker thread in onStartCommand()
    • Once started always run in the background
    • We have to manage the life of service ie call Stopself() once the job is done
  @Override  
   public int onStartCommand(Intent intent, int flags, int startId) {  
     Log.i(TAG, "Service onStartCommand " + startId);  
     final int currentId = startId;  
     Runnable r = new Runnable() {  
       public void run() {  
       }  
     };  
     Thread t = new Thread(r);  
     t.start();  
     return Service.START_STICKY;  
   }  

  • If you implement #2 then you are saved from this and the onHandleIntent() method will be actually called on the background thread and you can actually call long running background task on this thread
    • Once the onHandleIntent() is finished the service is automatically stopped
    • Requests are queued if multiple requests are sent at the same time
 
 @Override  
 protected void onHandleIntent(Intent intent) {  
   Log.d(TAG, "#### " + Thread.currentThread().getName());  
   if (intent != null) {  
     final String action = intent.getAction();  
     }  
  }  
 

Wednesday, September 2, 2015

Espresso Tests and Contrib Dependency

I struggled a lot in getting the tests compiled as soon as i added contrib in build.gradle to test navigation drawer support.

Please use below dependency in build.gradle if you dont wish to suffer with contrib-2-2

androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support:support-annotations:22.2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

Monday, August 24, 2015

Android Podcasts

Important Podcasts to Follow on Android.

There is insanely great information made available through these podcasts. I generally find it more appealing to listen to these podcasts cause you get to hear the views from top notch developers on android and their ideas about how few things can be implemented in more efficient manner.

I urge every budding developer to follow these podcasts

Wednesday, August 19, 2015

BroadCastReceiver and Worker thread

Don’t start a worker thread from a broadcast receiver

As soon as the onReceive () method of the broadcast receiver returns , the broadcastreciever object is marked for Garbage Collection . BroadCastReceiever Is really a transient object so don’t try to run long running task in the onReceive () itself.

Good thing would be to start a service rathan than starting a worker thread in broadcastreceiver class. 
Lets say if Broadcastreceiver was the only application component in memory then as soon as the onReceive () method returns , system thinks that its an empty process and might just kill the application in the interest of freeing up memory cause these are the ideal candidates to get killed first and system is unaware that you have started a worker thread .


Since service is an application component system will see that component running and will not touch it unless otherwise there is really no option.


System will try to keep that process around yey :-.after all isnt that what we want as an application developers

Sunday, August 16, 2015

Android Studio keyboard ShortCuts

Type less and code more 

    One of the things that can quickly get you out of the main stream of thought is typing lot of boiler plate code.

    I have recently started doing development in IntelliJ IDE and its really awesome [ I am still learning :-) ] and what i have realised is using these live templates really help me focus on the main algorithm rather than getting bogged down by the shear typing involved in implementing the code.

    I hope you will really like it.  

 Be friends with Alt + Enter / Alt + Insert

        These will be undoubtedly the most used keys .
         Alt + Enter  -. Refactor / Imports / TypeCast
         Alt + Insert  -  Getter / Setter / Constructor / Override Methods

 Find a particular class by its name : ctrl + n

 The cool thing thats happening here is you can actually write only camel letters and give a colon and
 a number to go to a particular line     
 

Find all recently edited files :double shift



If you are presenting use presenstation mode : View -> Enter presentation mode