Skip to main content

How to save and restore the state of a Spinner in Kotlin Android

How to save and restore the state of a Spinner in Kotlin Android.

Here's a step-by-step tutorial on how to save and restore the state of a Spinner in Kotlin Android:

Step 1: Create a new Android project

Create a new Android project in Android Studio. Set up the project with a basic activity layout containing a Spinner.

Step 2: Declare the Spinner and initialize it

In the activity's layout XML file, declare a Spinner widget. Give it an id, such as "spinner". In the activity's Kotlin file, initialize the Spinner using findViewById.

// In your activity's Kotlin file
private lateinit var spinner: Spinner

// Inside onCreate method
spinner = findViewById(R.id.spinner)

Step 3: Create an ArrayAdapter for the Spinner

To populate the Spinner with data, create an ArrayAdapter and set it as the adapter for the Spinner. You can use an array of strings or any other data source.

val data = arrayOf("Option 1", "Option 2", "Option 3")

val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, data)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

spinner.adapter = adapter

Step 4: Save the selected item position

To save the state of the Spinner, you need to save the selected item position. You can do this by overriding the onSaveInstanceState method of the activity.

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("spinner_position", spinner.selectedItemPosition)
}

Step 5: Restore the selected item position

To restore the state of the Spinner, you need to retrieve the saved selected item position. You can do this by overriding the onRestoreInstanceState method of the activity.

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val position = savedInstanceState.getInt("spinner_position")
spinner.setSelection(position)
}

Step 6: Test the functionality

Run the app and select an item from the Spinner. Rotate the device or recreate the activity (e.g., by minimizing and reopening the app). The selected item should be restored.

That's it! You have successfully saved and restored the state of a Spinner in Kotlin Android.

Here's the complete code for reference:

class MainActivity : AppCompatActivity() {

private lateinit var spinner: Spinner

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

spinner = findViewById(R.id.spinner)

val data = arrayOf("Option 1", "Option 2", "Option 3")

val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, data)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

spinner.adapter = adapter
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("spinner_position", spinner.selectedItemPosition)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val position = savedInstanceState.getInt("spinner_position")
spinner.setSelection(position)
}
}