add ActivityExt
This commit is contained in:
parent
8f803ff3b8
commit
8a237a1156
|
|
@ -31,6 +31,8 @@
|
|||
<activity android:name="luyao.util.ktx.ui.IntentExtActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.PermissionExtActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.SharedPreferencesActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.ActivityExtActivity"/>
|
||||
<activity android:name="luyao.util.ktx.ui.AnotherActivity"/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
@ -3,6 +3,7 @@ package luyao.util.ktx
|
|||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import luyao.ktx.R
|
||||
import luyao.util.ktx.base.BaseActivity
|
||||
import luyao.util.ktx.ext.startKtxActivity
|
||||
import luyao.util.ktx.mvvm.MvvmActivity
|
||||
import luyao.util.ktx.ui.ExtActivity
|
||||
|
||||
|
|
@ -13,7 +14,7 @@ class MainActivity : BaseActivity() {
|
|||
override fun initView() {}
|
||||
|
||||
override fun initData() {
|
||||
btMvvm.setOnClickListener { startActivity(MvvmActivity::class.java) }
|
||||
btExt.setOnClickListener { startActivity(ExtActivity::class.java) }
|
||||
btMvvm.setOnClickListener { startKtxActivity<MvvmActivity>() }
|
||||
btExt.setOnClickListener { startKtxActivity<ExtActivity>()}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
package luyao.util.ktx.ui
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import kotlinx.android.synthetic.main.activity_activity_ext.*
|
||||
import kotlinx.android.synthetic.main.title_layout.*
|
||||
import luyao.ktx.R
|
||||
import luyao.util.ktx.base.BaseActivity
|
||||
import luyao.util.ktx.ext.startKtxActivity
|
||||
import luyao.util.ktx.ext.startKtxActivityForResult
|
||||
import luyao.util.ktx.ext.toast
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
* on 2019/7/9 14:29
|
||||
*/
|
||||
class ActivityExtActivity : BaseActivity() {
|
||||
override fun getLayoutResId() = R.layout.activity_activity_ext
|
||||
|
||||
override fun initView() {
|
||||
mToolbar.title = "ActivityExt"
|
||||
}
|
||||
|
||||
override fun initData() {
|
||||
startActivity.setOnClickListener {
|
||||
startKtxActivity<AnotherActivity>()
|
||||
}
|
||||
|
||||
startActivityWithFlag.setOnClickListener {
|
||||
startKtxActivity<AnotherActivity>(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
|
||||
startActivityForResult.setOnClickListener {
|
||||
startKtxActivityForResult<AnotherActivity>(requestCode = 1024)
|
||||
}
|
||||
|
||||
startActivityWithValue.setOnClickListener {
|
||||
startKtxActivity<AnotherActivity>(
|
||||
values = arrayListOf(
|
||||
"int" to 1,
|
||||
"boolean" to true,
|
||||
"string" to "from previous activity"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
startActivityWithBundle.setOnClickListener {
|
||||
startKtxActivity<AnotherActivity>(
|
||||
extra = Bundle().apply {
|
||||
putInt("int",2)
|
||||
putBoolean("boolean",true)
|
||||
putString("string","from bundle")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
|
||||
if ((resultCode == Activity.RESULT_OK) and (requestCode == 1024)) {
|
||||
toast("onActivityResult")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package luyao.util.ktx.ui
|
||||
|
||||
import android.app.Activity
|
||||
import kotlinx.android.synthetic.main.activity_another.*
|
||||
import kotlinx.android.synthetic.main.title_layout.*
|
||||
import luyao.ktx.R
|
||||
import luyao.util.ktx.base.BaseActivity
|
||||
import luyao.util.ktx.ext.toast
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
* on 2019/7/9 14:32
|
||||
*/
|
||||
class AnotherActivity : BaseActivity() {
|
||||
|
||||
private val int by lazy { intent.getIntExtra("int", 0) }
|
||||
private val boolean by lazy { intent.getBooleanExtra("boolean", false) }
|
||||
private val string by lazy { intent.getStringExtra("string") }
|
||||
|
||||
override fun getLayoutResId() = R.layout.activity_another
|
||||
|
||||
override fun initView() {
|
||||
mToolbar.title = "AnotherActivity"
|
||||
|
||||
toast("$int\n$boolean\n$string")
|
||||
}
|
||||
|
||||
override fun initData() {
|
||||
finish.setOnClickListener {
|
||||
setResult(Activity.RESULT_OK)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ class ExtActivity : CommonListActivity() {
|
|||
|
||||
override fun initList() {
|
||||
dataList.run {
|
||||
add(ItemBean("ActivityExt",ActivityExtActivity::class.java))
|
||||
add(ItemBean("ToastExt", ToastExtActivity::class.java))
|
||||
add(ItemBean("AppExt", ToastExtActivity::class.java))
|
||||
add(ItemBean("IntentExt", IntentExtActivity::class.java))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<include layout="@layout/title_layout"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/startActivity"
|
||||
android:text="startActivity"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/startActivityWithFlag"
|
||||
android:text="startActivityWithFlag"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/startActivityForResult"
|
||||
android:text="startActivityForResult"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/startActivityWithValue"
|
||||
android:text="startActivityWithValue"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/startActivityWithBundle"
|
||||
android:text="startActivityWithBundle"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<include layout="@layout/title_layout"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/finish"
|
||||
android:text="finish"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
android:id="@+id/finish"
|
||||
android:text="finish"
|
||||
style="@style/NormalButton"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
package luyao.util.ktx.ext
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
* on 2019/7/9 14:17
|
||||
*/
|
||||
inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null): Unit =
|
||||
startActivity(getIntent<T>(flags, extra))
|
||||
|
||||
inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle?=null,values: List<Pair<String, Any>?>?): Unit =
|
||||
startActivity(getIntent<T>(flags, extra,values))
|
||||
|
||||
inline fun <reified T : Activity> Activity.startKtxActivityForResult(
|
||||
requestCode: Int,
|
||||
flags: Int? = null,
|
||||
extra: Bundle? = null
|
||||
): Unit =
|
||||
startActivityForResult(getIntent<T>(flags, extra), requestCode)
|
||||
|
||||
inline fun <reified T : Context> Activity.getIntent(flags: Int?, extra: Bundle?): Intent =
|
||||
Intent(this, T::class.java).apply {
|
||||
flags?.let { setFlags(flags) }
|
||||
extra?.let { putExtras(extra) }
|
||||
}
|
||||
|
||||
inline fun <reified T : Context> Activity.getIntent(flags: Int?, extra: Bundle?, pairs: List<Pair<String, Any>?>?): Intent =
|
||||
Intent(this, T::class.java).apply {
|
||||
flags?.let { setFlags(flags) }
|
||||
extra?.let { putExtras(extra) }
|
||||
if (pairs != null) {
|
||||
for (pair in pairs)
|
||||
pair?.let {
|
||||
val name = pair.first
|
||||
when (val value = pair.second) {
|
||||
is Int -> putExtra(name, value)
|
||||
is Byte -> putExtra(name, value)
|
||||
is Char -> putExtra(name, value)
|
||||
is Short -> putExtra(name, value)
|
||||
is Boolean -> putExtra(name, value)
|
||||
is Long -> putExtra(name, value)
|
||||
is Float -> putExtra(name, value)
|
||||
is Double -> putExtra(name, value)
|
||||
is String -> putExtra(name, value)
|
||||
is CharSequence -> putExtra(name, value)
|
||||
is Parcelable -> putExtra(name, value)
|
||||
is Array<*> -> putExtra(name, value)
|
||||
is ArrayList<*> -> putExtra(name, value)
|
||||
is Serializable -> putExtra(name, value)
|
||||
is BooleanArray -> putExtra(name, value)
|
||||
is ByteArray -> putExtra(name, value)
|
||||
is ShortArray -> putExtra(name, value)
|
||||
is CharArray -> putExtra(name, value)
|
||||
is IntArray -> putExtra(name, value)
|
||||
is LongArray -> putExtra(name, value)
|
||||
is FloatArray -> putExtra(name, value)
|
||||
is DoubleArray -> putExtra(name, value)
|
||||
is Bundle -> putExtra(name, value)
|
||||
is Intent -> putExtra(name, value)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,11 +13,11 @@ private enum class LEVEL {
|
|||
V, D, I, W, E
|
||||
}
|
||||
|
||||
fun String.v(tag: String = TAG, message: String) = log(LEVEL.V, tag, message)
|
||||
fun String.d(tag: String = TAG, message: String) = log(LEVEL.D, tag, message)
|
||||
fun String.i(tag: String = TAG, message: String) = log(LEVEL.I, tag, message)
|
||||
fun String.w(tag: String = TAG, message: String) = log(LEVEL.W, tag, message)
|
||||
fun String.e(tag: String = TAG, message: String) = log(LEVEL.E, tag, message)
|
||||
fun String.v(tag: String = TAG) = log(LEVEL.V, tag, this)
|
||||
fun String.d(tag: String = TAG) = log(LEVEL.D, tag, this)
|
||||
fun String.i(tag: String = TAG) = log(LEVEL.I, tag, this)
|
||||
fun String.w(tag: String = TAG) = log(LEVEL.W, tag, this)
|
||||
fun String.e(tag: String = TAG) = log(LEVEL.E, tag, this)
|
||||
|
||||
private fun log(level: LEVEL, tag: String, message: String) {
|
||||
when (level) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue