Posts

Random Android Learnings

Follow my Medium page at https://medium.com/@ghbhatt

Activity lifecycle on rotation
We all learn that on rotation the Activity undergoes destruction and re-creation. To handle this, we often have listeners / weak references to the Activity that is detached and reattached in the onStop() and onStart() methods. These listeners are invoked when a long running task returns e.g your network call.
In an interview, I was asked a question: what would happen if the call returned right at the time when the activity is destroyed but the new instance is not yet created? i.e what would happen when your long running task returned between the onStop() of the Activity being destroyed and onStart() of the Activity being recreated? At first, I was confused because it seemed like a really legit use case and I was wondering why I never thought about it. Needless to say, I didn't do well in that interview trying to give a really weird timing based solution but on the bright side, I learned a really valua…

Launch Intent Pattern

Often when developing Android apps, we come across the situation when we want to pass some data from ActivityA to ActivityB. To do so, we declare a key to be used to pass and retrieve the data in the Bundle. E.g. In ActivityB you might have something like:public static final KEY_USERNAME = "activityb.username";In ActivityA, you bundle the data like:
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra(ActivityB.KEY_USERNAME, userName);
startActivity(intent);In ActivityB, you'd read the data like: String userName = getIntent().getExtras().getString(KEY_USERNAME);Now, as you can see, the KEY_USERNAME we declared in ActivityB is all over the place. First of all, the access needs to be public since we need it in ActivityA, outside of the scope of ActivityB. The key, in theory, is only required in ActivityB, since that Activity cares for the data. And as you can visualize, as the app starts getting complex, you see all these variables being referenced in some Acti…

Communication between components

When building large complex apps, you find yourself wanting to communicate between components of your app. For example, when you inflate a Fragment with a custom view, you may want your Fragment to communicate with the View and/or vice versa. More than once, I have come across code like this in Android projects to solve this problem: // Custom View class public class MyView extends View { public void setAndDoSomething(Fragment fragment, String text) { if (text.contains("NO") { fragment.doSomething(); } } } // Fragment class public class MyFragment extends Fragment { View myView; public void onSomeAction() { myView.setAndDoSomething(this, "NO"); } public void doSomething() { //some logic } }As you can see, the whole Fragment object is passed to the view only for it to call a method doSomething() which lies in the MyFragment class. There are a few things that are not right here; what if we want …

Value of Education and Education of Values

Why be Educated? One of the great saints of India, Gunatitanand Swami [http://www.swaminarayan.org/gunatitanandswami/index.htm], has said," Gadhesegadha mile to karelatamlat, gnanisegnani mile to karegnankibaat." Imagine a world full of illiterate people! It would unarguably be an animal kingdom. Perhaps, what makes us elite from the animals, is Education. It is the education, which bridges the gaps between people from different communities, countries, races, and unites them into a cognizant group of so-called "intellectuals." As cited by Aristotle, "Education is an ornament in prosperity and a refuge in adversity." Not only does education benefit an individual, but it also produces a ripple, which extends to strengthen the nation by itself. This is why John F Kennedy said, "Education is the means to develop our greatest abilities because in each of us there is a private hope and dream which, fulfilled, can be translated into benefit for everyone and …

MY Pursuit

Ever since I have seen Will Smith inarticulate with happiness in the last scene of the movie "The Pursuit of Happyness", I always wondered, about this feeling being sensed within me. Luckily for me, I was through with this emotion.
It was the result of trimester VI at my college [MPSTME, NMIMS]. I was an average student until then; my CGPA being around 2.6/4. But sometimes, some realization gives us a new direction. The same was the case with me. Ever since I realized that I need a minimum of 3 CGPA to pursue my higher studies, I knew I had to get a 3.3 GPA every trimester going forward and also, another reason being, my guilt of not performing well. Nothing else was different in the trimester as compared to the last 5 trimesters, except for my perception. It was a different kind of motivation for me. I attended lectures regularly, I took the learning process seriously, gave all the internal tests fully prepared, etc. It was then I realized that it was not difficult as I us…