Friday 29 April 2016

AsyncTask In Android 


onPreExecute(), invoked on the UI thread before the task is executed.
This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing.
This step is used to perform background computation that can take a long time.
The parameters of the asynchronous task are passed to this step.
The result of the computation must be returned by this step and will be passed back to the last step.
This step can also use publishProgress(Progress...) to publish one or more units of progress. 
These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). 
The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. 
For instance, it can be used to animate a progress bar or show logs in a text field.

onPostExecute(Result), invoked on the UI thread after the background computation finishes. 
The result of the background computation is passed to this step as a parameter.

Thursday 28 April 2016

RecyclerView vs. ListView



With the advent of Android Lollipop, the RecyclerView made its way officially. The RecyclerView is much more powerful, flexible and a major enhancement over ListView. I will try to give you a detailed insight into it.
1) ViewHolder Pattern
In a ListView, it was recommended to use the ViewHolder pattern but it was never a compulsion. In case of RecyclerView, this is mandatory using the RecyclerView.ViewHolder class. This is one of the major differences between the ListView and the RecyclerView.
It makes things a bit more complex in RecyclerView but a lot of problems that we faced in the ListView are solved efficiently.
2) LayoutManager
This is another massive enhancement brought to the RecyclerView. In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView.
Now using a RecyclerView, we can have a
i) LinearLayoutManager - which supports both vertical and horizontal lists,
ii) StaggeredLayoutManager - which supports Pinterest like staggered lists,
iii) GridLayoutManager - which supports displaying grids as seen in Gallery apps.
And the best thing is that we can do all these dynamically as we want.
3) Item Animator
ListViews are lacking in support of good animations, but the RecyclerView brings a whole new dimension to it. Using the RecyclerView.ItemAnimator class, animating the views becomes so much easy and intuitive.
4) Item Decoration
In case of ListViews, dynamically decorating items like adding borders or dividers was never easy. But in case of RecyclerView, the RecyclerView.ItemDecorator class gives huge control to the developers but makes things a bit more time consuming and complex.
5) OnItemTouchListener
Intercepting item clicks on a ListView was simple, thanks to its AdapterView.OnItemClickListenerinterface. But the RecyclerView gives much more power and control to its developers by theRecyclerView.OnItemTouchListener but it complicates things a bit for the developer.
In simple words, the RecyclerView is much more customizable than the ListView and gives a lot of control and power to its developers.

Tuesday 9 February 2016

Difference between abstract class and interface ?


1) Abstract class can have abstract and non-abstract methods.
    Interface can have only abstract methods.

2) Abstract class doesn't support multiple inheritance.
    Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and non-static variables.
    Interface has only static and final variables.

4) Abstract class can have static methods, main method and constructor.
    Interface can't have static methods, main method or constructor.

5) Abstract class can provide the implementation of interface.
    Interface can't provide the implementation of abstract class.

6) The abstract keyword is used to declare abstract class.
    The interface keyword is used to declare interface.

Tuesday 2 February 2016

How to stop Asynctask Thread in Android ?


if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
{
    loginTask.cancel(true);
}
Where loginTask is object of your AsyncTask