Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Tuesday, April 19, 2016

Event bus in android

Before learning how to implement event bus on Android, lets try to understand what's event bus and a scenario where it can be helpful.

There are two similar concepts that we need to understand here
Observer pattern
This is a pattern of development in which your class or primary object (known as the Observable) notifies other interested classes or objects (known as Observers) with relevant information (events). Your class will actually have a list of interested class that it needs to inform.
pub-sub pattern
Objective of this pattern is exactly same as the above pattern BUT, the focus here is on broadcasting the event. It is up to the Observer to receive the information
One of the most important advantage of this anonymity is it is easy to "Decouple" the Observable and the Observers. In this case Observable is the Publisher and Observers are the Subscribers. Hence the name pub-sub.
To achieve this decoupling, we will be using a middleman who handles the communication between pub and sub. Square's Otto and Green Robot’s EventBus

Lets think of a scenario, where event bus can be useful. On a login activity, when a user clicks on login button, most of the time a call will be made to REST server to check the user's authentication. During this background activity, if you are using a third party HTTP client like retrofit, we seldom use AsyncTask class. Retrofit would make all the calls in background in an "async" way. Depending on the result from the REST server we have to take further actions. when all these are happening in background, obviously we need to show user a dialogue that says that we are processing the request. In this situation we can have a "subscriber" who would receive an "event" after the REST call is a success/failure.

I will be using Green Robot’s EventBus here, as it is very simple and has few additional feature than Otto. https://github.com/greenrobot/EventBus/blob/master/COMPARISON.md can be checked for more information.
You can implement Green Robot’s EventBus in 3 easy steps.

  1. Create a POJO with all the information that you need to send form a "publisher" to "subscriber". This is the event that will be broadcasted

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   
        }   
     }   
    

  2. Define Subscribers. This is as simple as adding a annotation for the method. Add the annotation @Subscribe for a method which will accept the event defined in the above step as a parameter.

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   
        }   
     }   
    

  3. Post the event

     public class MessageEvent {   
        public final String message;   
        public MessageEvent(String message) {   
          this.message = message;   
        }   
     }   
    


Also, do not forget to register/unregister the subscribers on activity start and stop
 public class MessageEvent {   
    public final String message;   
    public MessageEvent(String message) {   
      this.message = message;   
    }   
 }   

Monday, January 04, 2016

Use "ButterKnife" to make your life easier


Easiest way to bind an Android view to the class file is to use "ButterKnife". No more long findViewById()
Using "Butterknife" is as simple as the name suggests :)
  • On your app's build.gradle, add "compile 'com.jakewharton:butterknife:7.0.1"
  • To bind  a view element in the class, simply use 
    • @Bind(R.id.btnSubmit) Button btnSubmit; 
  • After setContentView(R.layout.blah_blah_activity); 
    • ButterKnife.bind(this);
For more information https://github.com/JakeWharton/butterknife

Thursday, November 19, 2015

Downgrading Cordova / Installing older Cordova version

Have you run into a situation where you upgraded Cordova and your application broke due to a third party plugin???
Here are the steps to install previous version of Cordova
  • cordova -v  would tell you the current version of cordova installed.
  • npm info cordova  would tell you the available versions of cordova in npm.
  • Suppose you want to install  v 5.3.3 then just follow the below commands
    • npm uninstall -g cordova  This will uninstall the current cordova verison
    • npm install -g cordova@5.3.3 This will install the version 5.3.3
  • Then you need to update your application to point to new cordova
    • cordova platform update android
    • cordova platform update ios
  • You can also update ionic as below
    • ionic lib update