Friday 3 June 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.

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.

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

Difference between an IntentService and a Service ?


When to use ?

The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.

The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).
How to trigger ?

The Service is triggered by calling method startService().
The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.
Triggered From

The Service and IntentService may be triggered from any thread, activity or other application component.

Runs On

The Service runs in background but it runs on the Main Thread of the application.
The IntentService runs on a separate worker thread.
Limitations / Drawbacks

The Service may block the Main Thread of the application.
The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
When to stop?

If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method).
The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf()
What is the difference between a regular bitmap and a nine-patch images ?
In general, a Nine_patch image allows resizing that can be used as background or other image size requirements for the target device. The Nine-patch refers to the way you can resize the image: 4 corners that are unscaled, 4 edges that are scaled in 1 axis, and the middle one that can be scaled into both axes

What is the base class for any android application ?


Application
REST vs SOAP

SOAP stands for Simple Object Access Protocol. REST stands for REpresentational State Transfer.

SOAP is a XML based messaging protocol and REST is not a protocol but an architectural style.

SOAP has a standard specification but there is none for REST.

Whole of the web works based on REST style architecture. Consider a shared resource repository and consumers access the resources.

Even SOAP based web services can be implemented in RESTful style. REST is a concept that does not tie with any protocols.

SOAP is distributed computing style and REST is web style (web is also a distributed computing model).

REST messages should be self-contained and should help consumer in controlling the interaction between provider and consumer(example, links in message to decide the next course of action). But SOAP doesn’t has any such requirements.

REST does not enforces message format as XML or JSON or etc. But SOAP is XML based message protocol.

REST follows stateless model. SOAP has specifications for stateful implementation as well.

SOAP is strongly typed, has strict specification for every part of implementation. But REST gives the concept and less restrictive about the implementation.

Therefore REST based implementation is simple compared to SOAP and consumer understanding.

SOAP uses interfaces and named operations to expose business logic. REST uses (generally) URI and methods like (GET, PUT, POST, DELETE) to expose resources.

SOAP has a set of standard specifications. WS-Security is the specification for security in the implementation. It is a detailed standard providing rules for security in application implementation. Like this we have separate specifications for messaging, transactions, etc. Unlike SOAP, REST does not has dedicated concepts for each of these. REST predominantly relies on HTTPS.

Above all both SOAP and REST depends on design and implementation of the application.
Pass ArrayList From OneActivity to AnotherActivity ?


public class DataWrapper implements Serializable 
     {
       private ArrayList<String> arrayList;
       public DataWrapper(ArrayList<String> data) 
        {
         this.arrayList = data;
        }
       public ArrayList<String> getArrayList() 
       {
        return this.arrayList;
       }
      }
 

    Intent intent = new Intent(SubCat1Activity.this,Full1Activity.class);
    intent.putExtra("data",new DataWrapper(MainData));
    startActivity(intent);

    DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("data");
    ArrayList<String> list = dw.getArrayList();