Avoiding cold starts And Working with Multiple Flavors

Cold starts

A cold start refers to an app’s starting from scratch: the system’s process has not, until this start, created the app’s process. Cold starts happen in cases such as your app’s being launched for the first time since the device booted, or since the system killed the app. This type of start presents the greatest challenge in terms of minimizing startup time, because the system and app have more work to do than in the other launch states.
Why we should avoid using Splash Screens on Android, arguing that they hurt the user experience, increase the size of the application, etc.
The key is creating a custom theme that overrides android:windowBackground, then replacing that custom theme with your standard theme before calling super.onCreate().
Assuming you have a theme called AppTheme, your launcher theme would be:

themes.xml

<style name=”AppTheme.ColdStart”>

<item name=”android:windowBackground”>@drawable/splash_bg</item>

</style>

Place “splash_bg” inside drawable folder:

splash_bg.xml

<layer-list

xmlns:android=”http://schemas.android.com/apk/res/android”

android:opacity=”opaque”>

<!– The background color, preferably the same as your normal theme –>

<item android:drawable=”@android:color/white”/>

<!– Your product logo – 144dp color version of your app icon –>

<item>

<bitmap

android:gravity=”center”

android:src=”@drawable/new_logo”/>

</item>

</layer-list>

The easiest way to transition back to your normal theme is to call setTheme(R.style.AppTheme) before super.onCreate() and setContentView():

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style. AppTheme);
super.onCreate(savedInstanceState);
// …
}
}

Flavors

Flavor is a very powerful feature available in the Android gradle plug-in that allows us to manage different “flavors” of an application.
It’s important to understand why and when you might need multiple flavors. As an app developer, you would like to release a given app using a new brand name, logo and/or theme, along with some minor feature differences. Using this to maintain app very quickly.
The other way to create separate projects for each version, which is far from ideal, and will lead to errors and projects not having the complete new changes and fetch issues.
Using multi flavors developer can easy way to build and maintain different version of app.
Another very important concept regarding building android apps, called “build types”. The default build types are debug and release.
If you have create two flavors, called free and promo, and two build types called debug and release, the Build Variants will be:
  • freeDebug
  • freeRelease
  • promoDebug
  • promoRelease
  • paidDebug
  • paidRelease
check out the Android Studio Build Variant more information.