-
-
Save passsy/3e6a12150af02120f8c6c156100277cc to your computer and use it in GitHub Desktop.
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) |
can you send me an example of a project that will use it that I can see why it does not work?
My mail: [email protected]
@emoonadev You have to call the extensions from a Context. Here's a simple example of launching an activity when a button is clicked (using Anko layout and helpers). Assumes there is an Activity class called MapActivity in the project and that you've added the extensions Kotlin to your project (get rid of the top line in the gist with the package name and replace with the package name of your app)
Examples:
class AnkoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AnkoActivityUI().setContentView(this)
}
fun launchOtherActivity() {
this.launchActivity<OtherActivity>()
}
}
class OtherClass {
fun launchOtherActivity(ctx: Context) {
ctx.launchActivity<OtherActivity>()
}
}
you can combine with anko's intentFor also, it will be simpler
How about fragments? I suggest slightly improve this functions:
inline fun <reified T : Any> Activity.launchActivity(
requestCode: Int = -1,
options: Bundle? = null,
noinline init: Intent.() -> Unit = {}) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
startActivityForResult(newIntent<T>(this, init), requestCode, options)
else
startActivityForResult(newIntent<T>(this, init), requestCode)
}
inline fun <reified T : Any> Context.launchActivity(
options: Bundle? = null,
noinline init: Intent.() -> Unit = {}) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
startActivity(newIntent<T>(this, init), options)
else
startActivity(newIntent<T>(this, init))
}
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
inline fun <reified T : Any> Fragment.launchActivity(
requestCode: Int = -1,
options: Bundle? = null,
noinline init: Intent.() -> Unit = {}) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
startActivityForResult(newIntent<T>(requireContext(), init), requestCode, options)
else
startActivityForResult(newIntent<T>(requireContext(), init), requestCode)
}
inline fun <reified T : Any> newIntent(context: Context, noinline init: Intent.() -> Unit = {}): Intent {
val intent = Intent(context, T::class.java)
intent.init()
return intent
}
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?
I use both. If you provide a requestCode
it calls startActivityForResult
otherwise startActivity
Yep, I did research, and it's funny what I found. StartActivity calls startActivityForResult internally; then, it means that we would say its the same.
public void startActivity(Intent intent, @nullable Bundle options) {
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
It's important to call the correct method though. startActivity
might be overridden or - unlikely - the internal implementation might change.
this work:
startActivity(Intent(this, AddUserActivity::class.java))
this no work:
launchActivity<AddUserActivity> { }
Why not work:
My manifest: