Android Passing of Bundle Double Arguments and Beware of Default Zero (Not Null)

Sep 30, 2018
If you didn't pass any bundle argument of Double and call arguments.getDouble on the receiving end, you shall get Zero(0) rather than null. To check if the value are actually passed in, call arguments.containsKey.

If you didn't pass any bundle argument of Double and call arguments.getDouble on the receiving end, you shall get Zero(0) rather than null. To check if the value are actually passed in, call arguments.containsKey.

class LocationPickerDialog : DialogFragment() {    companion object {        private const val TAG = "LocationPickerDialog"        private const val EXTRA_LAT = "lat"        private const val EXTRA_LNG = "lng"        fun newInstance(lat: Double? = null, lng: Double? = null): LocationPickerDialog {            val dialog = LocationPickerDialog()            val args = Bundle().apply {                lat?.let { putDouble(EXTRA_LAT, it) }                lng?.let { putDouble(EXTRA_LNG, it) }            }            dialog.arguments = args            return dialog        }    }    override fun onActivityCreated(savedInstanceState: Bundle?) {        super.onActivityCreated(savedInstanceState)        val lat = arguments?.run {            if (containsKey(EXTRA_LAT)) getDouble(EXTRA_LAT) else null        }        val lng = arguments?.run {            if (containsKey(EXTRA_LNG)) getDouble(EXTRA_LNG) else null        }    }}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.