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.