Tuesday, January 30, 2018

PARCELABLE VS SERIALIZABLE

  1. To pass primitive data types like string, integer, float, we use intents which is very direct.Add key name and the respective data type value in the Intents but for class objects,then we need to use Serialization or Parcelable.
  2. For marshalling and unmarshalling of java objects,Parcelable and Serialization are used.Java serialization or Anroid parcelable(pass object references between activities)
Serialization:
  1. Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. It is a marker interface as it converts an object into a stream using the Java reflection API.
  2. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API and this helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects during the stream conversation process.Due to this, the Serialization process is slow in comparison to Parcelable.
  3. Serialization, on the other hand, is a Java interface that allows users to implement the interface which gets marked as Serializable.
  4. Android developers prefer Parcelable over Serialization technique as Serialization is available in Java.


Parcelable:

  1. Parcelable is faster than Serialization makes it a preferred choice of approach while passing an object because Android Parcelable implementation allows objects to read and write from Parcels which can contain flattened data inside message containers.We can write custom code for marshaling and unmarshaling so it creates fewer garbage objects using Parcelable. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.
  2. If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class.
  3.   The first step is to override the writeToParcel() method and write all object members into parcel objects.
  4.   The second is to create a static Parcelable.Creator object to de-serialize the Java object. Parcelable is well documented in the Android SDK;
  5. In Parcelable, One of the main reasons was the fact that Parcelable is fully customizable, allowing developers to convert required data into Parcelable. 
  6. Parcelable examples:
            1)@Override 
protected void onCreate(Bundle savedIntanceState){
super.onCreate(savedInstanceState);
}
                    Bundle should implement Parcelable

            2) MyClass myClassObj=new MyClass();

Intent myIntent=new Intent();
myIntent.putExtra("myClassObj",myClassObj);
                 MyClass should implement Parcelable

Saturday, January 20, 2018

BUTTERKNIFE

Where to use butterknife

1)Drawables, String, Boolean, Integer, Color and Dimension resources.

2) static class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.image_view)
    ImageView imageView;
    @BindView(R.id.TextView)
    TextView text_view;
    
    ViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
    }
}
Note the same with fragments public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view=inflater.inflate(R.layout.xxx,container,false);
ButterKnife.bind(this,view);
return view;
}

3) View lists
@BindViews({ R.id.button_flash, R.id.button_timer, R.id.button_hdr})
List<Button> camControls;
Now we have all the views in one list. Lets write VISIBILITY setter for simple show/hide behavior.
i)final ButterKnife.Setter<View, Integer> VISIBILITY = new ButterKnife.Setter<View,Integer>() {
    @Override
    public void set(@NonNull View view, Integer value, int index) {
        view.setVisibility(value);
    }
};

ii)Lambda Expressions
final ButterKnife.Setter<View, Integer> VISIBILITY 
          = (view, value, index) -> view.setVisibility(value);
ButterKnife.apply(camControls, VISIBILITY, View.GONE);

5)Listener Binding

  • @OnCheckedChange
  • @OnClick
  • @OnLongClick
  • @OnEditorAction
  • @OnFocusChange
  • @OnItemClick
  • @OnItemLongClick
  • @OnTouch
  • @OnTextChanged