Skip to main content
Fixed markdown
Source Link
Machavity
  • 31.8k
  • 27
  • 97
  • 108

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

###Life cycle graph

Life cycle graph

Image from here

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here } 

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

###Life cycle graph

Image from here

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here } 

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

Life cycle graph

Image from here

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here } 
Added citation
Source Link
Machavity
  • 31.8k
  • 27
  • 97
  • 108

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

###Life cycle graph

Image from here

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here } 

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

###Life cycle graph

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here } 

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

###Life cycle graph

Image from here

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here } 
Source Link
Sazzad Hissain Khan
  • 40.7k
  • 41
  • 213
  • 303

#Kotlin# You must override onSaveInstanceState and onRestoreInstanceState to store and retrieve your variables you want to be persistent

###Life cycle graph

Store variables

public override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) // prepare variables here savedInstanceState.putInt("kInt", 10) savedInstanceState.putBoolean("kBool", true) savedInstanceState.putDouble("kDouble", 4.5) savedInstanceState.putString("kString", "Hello Kotlin") } 

###Retrieve variables

public override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) val myInt = savedInstanceState.getInt("kInt") val myBoolean = savedInstanceState.getBoolean("kBool") val myDouble = savedInstanceState.getDouble("kDouble") val myString = savedInstanceState.getString("kString") // use variables here }