App Development

Android Development Tidbits // No. 8

Welcome to another round of Android tidbits. We hope you learn something new about development today.

Tidbit One If you use startActivityForResult() from a Fragment, the parent Activity will change the request code provided by you, by bit shifting the Fragment’s ID into the request code. It does this so that the parent Activity knows which child Fragment to return the result to. First the parent Activity will receive the onActivityResult() callback with the modified request code, then the child Fragment will receive onActivityResult() with the correct request code (with the Fragment ID bit shifted back out). This means that if you startActivityForResult() from a Fragment, and then override onActivityResult() in the parent Activity, you will not get the same request code back that you used. The correct use case is to both call startActivityForResult() and override onActivityResult() in the same class (either both in the Fragment, or both in the Activity, but don’t put one in each).

-Tidbit Contributor, Austen Lux

Tidbit Two If you change your application ID, you also have to update any content-provider “authorities” you have, or else stuff will break, because it doesn’t get overridden.

-Tidbit Contributor, Frank Doyle

Tidbit Three If you open up the “Search Everywhere” dialog in Android Studio (by double-tapping the Shift key), you can put the name of the module before the name of the file you are looking for, in order to refine your search more. For example, "module/strings” instead of just “strings.” This is especially handy if you are working in a repository with many different modules. -Tidbit Contributor, Austen Lux

Tidbit Four The EditText cursor does not show up in Android 4.1 - 4.4 if you set a custom background on the view. A workaround for this is to add android:textCursorDrawable="@null", which forces the cursor to be the same color as the TextColor attribute.

-Tidbit Contributor, Evan Tatarka

Tidbit Five If you want to provide your custom views with their dependencies (via the constructor) and still inflate them, use a LayoutInflaterFactory from the support library. \ *NOTE : You will lose your appcompat widgets if you don’t call AppCompatDelegate#createView() from your inflater when you don’t find your custom view.

-Tidbit Contributor, Sean Amos

Tidbit Six To skip specific unit tests (e.g. network tests that you only want to run manually) add

testOptions {
    unitTests.all {
        exclude 'com/example/app/android/api/ApiNetworkTest.class'

to the Android block of your module’s build.gradle.

-Tidbit Contributor, Sean Kenkeremath

Tidbit Seven If you’re serving mock data using MockWebServer or otherwise, make sure you’re setting the right Content-Length header in your response, or the HttpClient may not read all of the bytes in your response body.

-Tidbit Contributor, James Sun

Moving from Monolith to Microservices Architecture

When a client decides to move from a monolith platform to microservice architecture,...

Read the article