Some Pitfalls to avoid

Common pitfalls:


When we start an activity to perform a task not defined by our app such as invoking an implicite intent to handle send email , in such scenario sometimes there is no any activity registered to perform such a task , and startActivity raise an exception:

 Intent intent = new Intent();

startActivity(intent) // Exception if no register app found in system


send email In this situation you can use resolveActivity method of Intent class like:

PackageManager pm = context.getPackageManager();

if(intent.resolveActivity(pm) != null) {



send email In the case when an Activity from your own app may handle the task , your call would be Explicite It would be better off to check Export: 

ActivityInfo aInfo = intent.resolveActivityInfo(pm,intent.getFlags());


Using AsyncTask for long processing

AsyncTask life cycle is not linked to Activity life cycle in which it’s started , when for some reason Activity is recycled and removed from memory , AsyncTask may still running and reporting results to activity (which has been already recycled and views ID not valid anymore) , this scenario may happens in many use cases , suppose user’s screen orientation is changed and Activity recreated! this would be worth if you keep a pointer (reference) in AsyncTask to activity which may also lead to a Memory leak.

Although there is many solution following the context and situation in which the issue occurs,but in general you can use a service or IntentService instead of AsyncTask to prevent the issue for long processing or 

 © Xosrov 2016