Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Forked from passsy/KIntent.kt
Created February 27, 2018 20:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wajahatkarim3/ebe01a5fe57c15b83c772a65da301bff to your computer and use it in GitHub Desktop.
Save wajahatkarim3/ebe01a5fe57c15b83c772a65da301bff to your computer and use it in GitHub Desktop.
Kotlin extension functions to start a generic Activity
package com.pascalwelsch.extensions
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
/**
* Extensions for simpler launching of Activities
*/
inline fun <reified T : Any> Activity.launchActivity(
requestCode: Int = -1,
options: Bundle? = null,
noinline init: Intent.() -> Unit = {}) {
val intent = newIntent<T>(this)
intent.init()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
startActivityForResult(intent, requestCode, options)
} else {
startActivityForResult(intent, requestCode)
}
}
inline fun <reified T : Any> Context.launchActivity(
options: Bundle? = null,
noinline init: Intent.() -> Unit = {}) {
val intent = newIntent<T>(this)
intent.init()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
startActivity(intent, options)
} else {
startActivity(intent)
}
}
inline fun <reified T : Any> newIntent(context: Context): Intent =
Intent(context, T::class.java)
@wajahatkarim3
Copy link
Author

Usage

class OtherActivity: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //...
        userList.clickListener { user ->

            // super simple, no arguments for simple Activities but doesn't prevent a crash for this one
            launchActivity<UserDetailActivity>()

            // add the required argument
            launchActivity<UserDetailActivity> {
                putExtra(INTENT_USER_ID, user.id)
            }

            // add custom flags
            launchActivity<UserDetailActivity> {
                putExtra(INTENT_USER_ID, user.id)
                addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            }

            // use options for cool animations
            val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, avatar, "avatar")
            launchActivity<UserDetailActivity>(options = options) {
                putExtra(INTENT_USER_ID, user.id)
            }
            
            // requestCode, why not!?
            launchActivity<UserDetailActivity>(requestCode = 1234) {
                putExtra(INTENT_USER_ID, user.id)
            }
        }
    }
}

@denebchorny
Copy link

Why do you use startActivityForResult instead of startActivity?

IDK, but would it be good to be able to choose which one to use?

For example, one fun launchActivity and another one fun launchActivityForResult.

What do you think? Or is it right to use alwaysstartActivityForResult?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment