Saturday 21 September 2019

Android LiveData

Hello guys. here i m sharing information about Live Data. updates in Android.

- Lets see how it will help full for Developing Application.

- LiveData usefull for observable Data Class which holds all the Data.

- You can create singleton object of  LiveData and can share that common Data between Activity, Fragments , Services.

Life-cycle of Data with

Start and Resume.

- LiveData notifies when any Update happen in Data under Observe method.

- So now to bind view controls with LiveData objects , no need to worried about do any other update operation to get updated value bind with Ui.

- All Views related to bind with LiveData objects  will automatically get up to date with updated values.

Advantage Of LiveData in Application.

- All Time up to Date Data with all view-Controls.

- Automatically clean up there Data, so no Memory-Leak issue more.

- when Activity press back then LiveData goes inActive state. So Its avoid to crash Application.

- Its can use with ViewModel, Room etc where you require to use it in proper way.


Monday 9 September 2019

Android Data Binding Updates

Hello Guys i m updating regarding Data Binding features given in  Android Jetpack libraries

Here how we can implement Data Binding in Android its given.

-> This Library allows bind Layouts components with data in xml declaration instead of bind data and ui controls programmatically.

->  Example use the Data Binding  to assign textView in Xml layout.

->  No need to call other Java code

->  use this syntax @{} in assignment of control

Like

<TextView
    android:text="@{pojomodel.userName}" />

do in build.gradle file

android {
...
dataBinding {
    enabled = true}
}

->  in Details under stand like below
-> Create Pojo class.

package com.t.databinding;

public class Pojomodel {

    private String mid;
    private String strname;


    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public Pojomodel(String id, String name) {
        this.mid = id;
        this.strname = name;
    }

    public String getName() {
        return strname;
    }

    public void setName(String name) {
        this.strname = name;
    }

}


-> Create Layout file like


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <!--Step 1-->
    <data>
        <!--Step 2-->
        <variable
            name="Pojomodel"
            type="com.t.databinding.Pojomodel" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp"
        tools:context="com.androidbytes.databindingdemo.MainActivity">
        <!--Step 3-->
        <TextView
            android:id="@+id/text_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Pojomodel.mid}"
            android:textColor="@android:color/black"
            android:textSize="26sp" />
        <TextView
            android:id="@+id/text_view_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Pojomodel.name}"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
    </LinearLayout>
</layout>

-> Create  MainActivity

package com.t.databinding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import com.t.databinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //        After Data Binding
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        Pojomodel personVO = new Pojomodel("#1","Abc ");
        binding.setPojomodel(personVO);

    }
}


-> Data Binding  Use in  Application improve app performance , handle null pointer exception,  
    prevent from memory leak
-> Thanks this detail will help full to you for understand about Data Binding usage in Android Application.





Saturday 7 September 2019

About WorkManager

      Hello guy's here i m going to update some short important points about §  Work Manager will help to understand easily 

§  To perform single and multiple task with enqueue process manage using  Work Requests

§   Now Work Manager with Android Architecture Component and Its comes with Android Jetpack to handle  like  background work thread, and its  guaranteed to  execute to take care of perform Logic within it. It will do your background task  as it can.
      
      What benefits to be use of Work Manager

Its Support for asynchronous one and periodic tasks


Its Support for constraints like network conditions, storage space, and charging status

Its can Chaining of complex work requests, with  running work in parallel   

From one work request Get Output and Its used as input for the next

Compatibility back to API level supports Android 4.0+ (API 14+).

Works with or without Google Play services

Follows system health best practices

       Live Data support to easily display work request state in UI

When need to be use Work Manager

The Work Manager library is a good choice for perform long running tasks that are need to complete, even if the user navigates away from a particular Applications screen
Like below some tasks for good use of Work Manager
  • Applying filtration  to images and save image
  • On Particular Interval time  syncing local data with the network Data
  • Uploading Logs, Data

What you will  need to handle in Work Manager library 


  •  Worker -> You need to get subclass, to perform the task,  that will run in the background.

  •   Work Request -> Its  represents the request of the task that will run. Need to set the task through                                      the Worker subclass. Also You can specify constraints Like, only run the task if                                  the  device has Internet connectivity. There are two main types of work requests: 

  • One Time Work Request -> which can be executes the task just one time,


  • Periodic Work Request -> which can be executes your task periodically. 

                                         This Periodic Work Request can be customized 
                                          as per your needed interval.
  •  Work Status  ->    Whenever you want to know the status of  your  
                            work Request task, running, enqueued, finished etc,

                                 you can ask its to the Work Manager for it. 
                                So It will  provide a Live Data object containing 
                                one or more Work Status objects.

What you will  learn from Work Manager library

  • Passing data Input and output parameters
  • Multiple works request Chaining work
  • Unique work
  • Displaying work status in the UI
  • Adding Work Manager into your project
  • Scheduling a simple Single task execution
  • Cancelling work by Id





Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...