Best Practices

In AndroidPub. More on Medium.


Let’s take a look at a sample AsyncTask. It will make a network request to a URL and then return whether or not it was successful. For this example I’m using OkHttp, but that part isn’t important.

If you’ve used an AsyncTask before this code should look familiar. Inside the doInBackground method I make a network request to some given URL, and return the result as a Boolean. Then in onPostExecute I could do something with that result.

Now if I want to remove the AsyncTask and replace it with RxJava (and Retrolambda), one solution would look like this:


There is a common pattern I use in all of my RecyclerView adapters to listen for click events on each item.

I created an interface that is similar to the normal click listener, except it also has the position as a parameter.

With this interface I can set up a view holder class as a click listener, and pass in an instance of my interface. Then I set the view itself as a click listener and call my interface with the appropriate position.

In your case you may not want to set the entire view as a click…


Android provides you with a bunch of common views and ways to modify them. Sometimes you need to set a few of the same properties on the same types of views throughout your app. In that case you could make a layout and use the include tag to include it in every layout you need it in. So you can re-use it wherever you need it.

That may be fine, but what if you want to change a property on that view in one place it’s included? For example, you want to change the text colour of a TextView inside…


One of the most common patterns in app development is to have a LoginActivity which transitions to the MainActivity. Once the user has entered the correct credentials, the app starts the MainActivity and finishes the LoginActivity. The problem is, next time when the user launches your app, you don’t want them to have to login again.

There’s a few different approaches you can take, the easiest one being just force them to login every time they use your app. But, that’s a pretty horrible user experience. …


Hi guys, i work as a programmer more than 15 years and was using many different languages, paradigms, frameworks and other shit. And i want to share with you my rules of writing good code.

  1. Optimization VS Readability. Fuck the optimization.
    Always write code that simple to read and which will be understandable for developers. Because time and resources that will be spent on hard readable code will be much higher than what you get from optimization. …

AndroidPub

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store