Part 1. Advanced Android Tutorial - Introduction

What this tutorial expects you to know

So let's set out the pre-requisites before we proceed together on this journey. Something like a prenuptial agreement 😛. You should know Java (stuff like OOP, Inheritance, Interface etc.) and should have developed some Android app, even if as a hobby or learning exercise. In summary, you should have at least 1-2 years of Java experience, and some prior experience of developing Android apps. The sample application that I have written for this tutorial extensively uses Retrofit library for fetching data from an API. In this tutorial, I will not teach Retrofit library and so the tutorial assumes you are familiar with it. If that is not the case, you can still follow this tutorial. Go through this tutorial and write a simple app with Retrofit and you will be good to go!

What I do not expect you to know

I assume no previous exposure to Dependency Injection, RxJava or MVP. If you are just familiar with these concepts, this tutorial would give you an opportunity to deepen your understanding. If you are an expert, don't waste your time! No need to follow this tutorial. Make your own tutorial, publish it so that I can learn something from you 😜. The tutorial will explain these topics in connection with the Android domain only. These concepts are applicable to other domains as well, but those are not discussed here. Now, the most important part. The code of the application can be found in this Github repository. 

The Sample Application

The sample application uses Java 7. Initially, I wanted to write the application in Kotlin. But still very few people use Kotlin for Android. Also, handling both Kotlin and concepts introduced in this tutorial would have been extremely difficult. I don't want you to pull your hair out while reading this tutorial So, I stuck with Java and also avoided Retrolambda.



The sample app is a very silly app with no real world use in my opinion. The entry screen shows you weather information after reading your location. It uses OpenWeatherMap API and RxJava for fetching weather information and presenting it on the screen.  The second screen displays the notes that you have added. There is a button at the bottom right which you can click to open a screen and add a new note. Who would want that kind of an app! So it is not released on the Play Store. It only lives in Github hidden from the world and protected from all the mockings that it would surely get if it ventures out 😪.

After reading this tutorial, you may not become a fan of MVP and Dependency Injection, but you would surely love RxJava! It makes handling asynchronous calls very easy compared to AsyncTask or other mechanisms. 

Though AsyncTask was introduced as "Painless threading", but nothing can be farther from the truth. There is hardly any situation where you should use it in any application.

While AsyncTask is surely "Painful threading", handling network calls with Handlers and Runnables is not that bad. But the main risk of Handlers is memory leakage. To avoid it, programmers create handlers statically so that it does not hold a reference to the activity. Then they use WeakReference to update the user interface without creating a memory leak. But handling multiple network calls using this approach is very difficult. You would appreciate how RxJava makes it easier to handle multiple network calls and its implementation is far simpler and less bug-prone than using handlers.





No comments:

Powered by Blogger.