Posts

Showing posts from August, 2019

The switch-case fall through

So, I am a Software Engineer at a reputed firm and still fell for the trap of fall-through feature of the switch-case statement.

Rule of thumb: Starting from the matched case, all statements are executed, unless a break statement is reached. After the break statement, the control is transferred to the statement next to the switch-case block. 

Here are some examples to test your understanding of switch-case:

The output of the example is AB. This is because there is no break statement and so all the statements are executed after the first match of case 0.

The output of the example is BCD. This is because there is no break statement and so all the statements are executed after the first match of case 1.
The output of the example is AB. This is because there is a break statement at the end of case 1 so, after the first match at case 0, all the statements are executed till the break statement.

The output of the example is AB. This is because there is a break statement at the end of case 2…

Constructor of a Fragment in Android

Recently, I came to a very strange issue in Android development and I wish to share a very important lesson. As an Android developer, you might be knowing that most of the crashes are related to lifecycle events of Activities and Fragments. Moreover, most phone manufacturers patch the original Android system to come up with their own versions. Such versions might have a hostile kernel which may kill Activities more frequently to claim more free space and faster performance for foreground apps. This makes Android development difficult as you have to check for each of the edge cases that may occur.

One of the best ways to debug such crashes is to use the "Don't Keep Activities" feature of the development mode. This kills the Activities and the fragments as soon as the app goes to the background. Enable this mode and start your app and navigate through all your Activities and Fragments. If you get no faulty behaviour, then your app is of quality-at-par.
Let's take a cl…