Create Static Method for Android Activity Intent (Kotlin)

Jun 18, 2018
Start Activity With Extra Parameter - newInstance
class SharePreviewActivity : AppCompatActivity() {    companion object {        private const val EXTRA_MESSAGE = "message"        fun newInstance(context: Context, message: String? = null) = Intent(context, {            putExtra(EXTRA_MESSAGE, message)        }    }    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_share_preview)        setSupportActionBar(toolbar)        val message = intent.getStringExtra(EXTRA_MESSAGE)    }}
startActivity(SharePreviewActivity.newInstance(context))startActivity(SharePreviewActivity.newInstance(context, "Hello"))


❤️ 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.