Create Static Method for Android Activity Intent (Kotlin)

June 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, SharePreviewActivity::class.java).apply {
            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"))

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.