Bird
Raised Fist0
Android Kotlinmobile~20 mins

Remote Config in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Remote Config Demo
This screen fetches and applies remote configuration values to update the UI dynamically without app updates.
Target UI
-------------------------
| Remote Config Demo    |
|-----------------------|
| Welcome message:      |
| "Loading..."          |
|                       |
| Button: Fetch Config  |
-------------------------
Display a welcome message text that updates based on remote config value.
Add a button labeled 'Fetch Config' that fetches remote config values when tapped.
Use Firebase Remote Config to fetch a string value with key 'welcome_message'.
Show 'Loading...' initially before fetching config.
Update the welcome message text with the fetched value or default if fetch fails.
Starter Code
Android Kotlin
package com.example.remoteconfigdemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    private lateinit var welcomeTextView: TextView
    private lateinit var fetchButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        welcomeTextView = findViewById(R.id.welcomeTextView)
        fetchButton = findViewById(R.id.fetchButton)

        welcomeTextView.text = "Loading..."

        fetchButton.setOnClickListener {
            // TODO: Fetch remote config and update welcomeTextView
        }
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.remoteconfigdemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings

class MainActivity : AppCompatActivity() {
    private lateinit var welcomeTextView: TextView
    private lateinit var fetchButton: Button
    private lateinit var remoteConfig: FirebaseRemoteConfig

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        welcomeTextView = findViewById(R.id.welcomeTextView)
        fetchButton = findViewById(R.id.fetchButton)

        // Initialize Firebase Remote Config
        remoteConfig = FirebaseRemoteConfig.getInstance()

        // Set default values
        val defaults = mapOf("welcome_message" to "Welcome to the app!")
        remoteConfig.setDefaultsAsync(defaults)

        welcomeTextView.text = "Loading..."

        fetchButton.setOnClickListener {
            // Fetch remote config values
            remoteConfig.fetchAndActivate().addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    val message = remoteConfig.getString("welcome_message")
                    welcomeTextView.text = message
                } else {
                    welcomeTextView.text = "Welcome to the app!"
                }
            }
        }
    }
}

We start by initializing the Firebase Remote Config instance. Then, we set a default value for the key "welcome_message" so the app has a fallback message.

When the user taps the 'Fetch Config' button, the app calls fetchAndActivate() to get the latest remote config values from Firebase.

If fetching succeeds, we update the welcome message text with the fetched value. If it fails, we show the default welcome message.

This approach lets the app update text dynamically without needing an app update.

Final Result
Completed Screen
-------------------------
| Remote Config Demo    |
|-----------------------|
| Welcome message:      |
| "Welcome to the app!" |
|                       |
| Button: Fetch Config  |
-------------------------
Initially, the welcome message shows 'Loading...'.
When user taps 'Fetch Config', the app fetches remote config from Firebase.
If successful, the welcome message updates to the fetched string.
If fetch fails, the welcome message shows the default text.
Stretch Goal
Add a loading indicator that shows while fetching remote config values.
💡 Hint
Use a ProgressBar view and toggle its visibility before and after fetch.

Practice

(1/5)
1. What is the main purpose of Firebase Remote Config in an Android app?
easy
A. To manage user authentication and login
B. To store user data locally on the device
C. To change app behavior and appearance without releasing a new app version
D. To handle app crashes and error reporting

Solution

  1. Step 1: Understand Remote Config functionality

    Remote Config allows developers to update app behavior remotely without requiring users to download a new version.
  2. Step 2: Compare options with Remote Config purpose

    Options B, C, and D relate to other Firebase services, not Remote Config.
  3. Final Answer:

    To change app behavior and appearance without releasing a new app version -> Option C
  4. Quick Check:

    Remote Config = Change app behavior remotely [OK]
Hint: Remote Config updates app without new installs [OK]
Common Mistakes:
  • Confusing Remote Config with local data storage
  • Mixing Remote Config with authentication features
  • Thinking Remote Config handles crash reports
2. Which Kotlin code snippet correctly fetches and activates Remote Config values asynchronously?
easy
A. remoteConfig.fetchAndActivate().addOnCompleteListener { task -> if (task.isSuccessful) { applyConfig() } }
B. remoteConfig.fetch().activate()
C. remoteConfig.activate().fetch()
D. remoteConfig.fetchAndActivate { applyConfig() }

Solution

  1. Step 1: Identify correct method chaining for fetch and activate

    The correct method is fetchAndActivate() which returns a Task to listen for completion.
  2. Step 2: Check syntax for asynchronous handling

    remoteConfig.fetchAndActivate().addOnCompleteListener { task -> if (task.isSuccessful) { applyConfig() } } uses addOnCompleteListener properly to handle success and then apply config.
  3. Final Answer:

    remoteConfig.fetchAndActivate().addOnCompleteListener { task -> if (task.isSuccessful) { applyConfig() } } -> Option A
  4. Quick Check:

    fetchAndActivate() with listener = remoteConfig.fetchAndActivate().addOnCompleteListener { task -> if (task.isSuccessful) { applyConfig() } } [OK]
Hint: Use fetchAndActivate() with addOnCompleteListener [OK]
Common Mistakes:
  • Calling activate() before fetch()
  • Using fetch() and activate() separately without chaining
  • Passing a lambda directly to fetchAndActivate()
3. Given this Kotlin code snippet, what will be printed if the Remote Config parameter "welcome_message" is not set remotely and default is "Hello"?
val welcome = remoteConfig.getString("welcome_message")
println(welcome)
medium
A. Hello
B. null
C. An empty string
D. welcome_message

Solution

  1. Step 1: Understand getString behavior with default values

    If the parameter is not set remotely, Remote Config returns the default value provided in app defaults.
  2. Step 2: Recognize default value is "Hello"

    The default for "welcome_message" is "Hello", so getString returns "Hello".
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    Missing remote value returns default string [OK]
Hint: Missing remote key returns default value [OK]
Common Mistakes:
  • Expecting null instead of default
  • Assuming empty string if not set
  • Confusing key name with value
4. What is wrong with this Kotlin code snippet for fetching Remote Config values?
remoteConfig.fetchAndActivate()
println(remoteConfig.getString("feature_flag"))
medium
A. fetchAndActivate() must be called on the main thread
B. feature_flag key must be an integer, not string
C. fetchAndActivate() requires a callback parameter
D. getString() is called before fetchAndActivate() completes

Solution

  1. Step 1: Analyze asynchronous fetchAndActivate call

    fetchAndActivate() runs asynchronously, so calling getString immediately after may use old values.
  2. Step 2: Identify missing completion handling

    Code lacks a listener or await to ensure fetchAndActivate finished before reading values.
  3. Final Answer:

    getString() is called before fetchAndActivate() completes -> Option D
  4. Quick Check:

    Must wait for fetchAndActivate() completion [OK]
Hint: Wait for fetchAndActivate() to finish before reading values [OK]
Common Mistakes:
  • Assuming fetchAndActivate() is synchronous
  • Not adding completion listeners
  • Confusing key types
5. You want to show a special promotion banner only to 30% of your users using Remote Config. Which approach is best to implement this in Kotlin?
hard
A. Fetch a random number in app and show banner if number < 0.3
B. Create a Remote Config parameter "show_promo" with boolean true/false and use Firebase percentage rollout targeting
C. Use a local SharedPreferences flag to randomly enable banner for 30% users
D. Hardcode banner visibility in app and update app version for changes

Solution

  1. Step 1: Understand Remote Config targeting features

    Firebase Remote Config supports percentage rollouts to control feature exposure to a subset of users.
  2. Step 2: Choose parameter with percentage rollout

    Setting a boolean parameter "show_promo" with percentage targeting is the best practice for controlled rollout.
  3. Final Answer:

    Create a Remote Config parameter "show_promo" with boolean true/false and use Firebase percentage rollout targeting -> Option B
  4. Quick Check:

    Use Remote Config percentage rollout for user targeting [OK]
Hint: Use Remote Config percentage rollout, not local random logic [OK]
Common Mistakes:
  • Using local random logic instead of Remote Config targeting
  • Hardcoding feature flags requiring app updates
  • Ignoring Remote Config's built-in user segmentation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard