Learn Kotlin for Android Development by Peter Späth

Learn Kotlin for Android Development by Peter Späth

Author:Peter Späth
Language: eng
Format: epub
ISBN: 9781484244678
Publisher: Apress


restoreData(savedInstanceState) // new!

showData(Statistics.getStatistics())

}

The two new functions read:fun restoreData(savedInstanceState: Bundle?) {

savedInstanceState?.run {

getSerializable("statistics.data")?.run {

Statistics.data.clear()

Statistics.data.addAll( this as

ArrayList<Statistics.GameSessionRecord>)

}

}

}

override fun onSaveInstanceState(outState: Bundle?) {

super.onSaveInstanceState(outState)

outState?.putSerializable("statistics.data",

Statistics.data)

}

The ?.run{ } constructs make sure the run block gets executed only if the value in front of it is not null. If it gets executed, this contains exactly this value. Therefore the getSerializable() actually translates to this.getSerializable() and thus to the getSerializable() from the savedInstanceState object. The this as ... is needed because the getSerializable() by language design has lost its type information, so we must explicitly state the type.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.