我正在尝试将Intent Activity Utils作为Kotlin中的扩展函数来实现。他们将从其他活动/片段开始活动。请向我建议最好的方法。
我试着解决这样的问题:
在"活动"中,我调用如下扩展函数:
when (view.id) {
PROFILE.actionViewId -> {
// Start Activity normally
startNewActivity(MyProfileActivity::class.java)
}
PREPAID_RECHARGE.actionViewId -> {
// Dont start activity and crash
startNewActivityForResult(RechargePrepaidActivity::class.java, RECHARGE_PREPAID_REQUEST_CODE)
// Start Activity normally
this.startActivityForResult(Intent(this, RechargePrepaidActivity::class.java), RECHARGE_PREPAID_REQUEST_CODE)
}
TROUBLE_TICKETS.actionViewId -> {
startNewActivity(TroubleTicketsActivity::class.java) // Start Activity normally
}
HELP.actionViewId -> {
startNewActivity(HelpSectionActivity::class.java) // Start Activity normally
}
IntentTils.kt:
fun <T : BaseActivity> BaseActivity.startNewActivity(newActivity: Class<T>) {
val intent = Intent(this, newActivity)
this.startActivity(intent)
}
//fun BaseActivity.startNewActivity(newActivity: Class<*>) {
// val intent = Intent(this, newActivity)
// this.startActivity(intent)
//}
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java)
startActivityForResult(intent, requestCode)
}
fun BaseFragment.startNewActivity(newActivity: Class<*>) {
val intent = Intent(this.requireActivity(), newActivity::class.java)
this.startActivity(intent)
}
fun BaseFragment.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this.requireActivity(), newActivity::class.java)
startActivityForResult(intent, requestCode)
}
此外,活动在清单中声明-Kotlin,Android
<activity
android:name=".ui.recharge.RechargePrepaidActivity"
android:configChanges="locale|orientation|screenSize"
android:screenOrientation="portrait" />
这会正常启动活动:
startNewActivity(MyProfileActivity::class.java) // use extend func
startActivity(Intent(this, MyProfileActivity::class.java)) // use inherited Activity func
但是,当我试图通过请求代码创建相同的函数来启动结果的活动时,它不起作用,当我使用继承的活动函数startActivityForResult()
时,也没问题
// Dont start activity and crash
startNewActivityForResult(RechargePrepaidActivity::class.java, RECHARGE_PREPAID_REQUEST_CODE)
// Start Activity normally
startActivistyForResult(Intent(this, RechargePrepaidActivity::class.java), RECHARGE_PREPAID_REQUEST_CODE)
奇怪的是,当我使用IntentUtils.startNewActivityForResult()
应用程序时,会与ActivityNotFoundException
一起崩溃,但在正常使用Fragment.startActivityForResult()
时,它没有这种行为:
Caused by: android.content.ActivityNotFoundException:
Unable to find explicit activity class {/java.lang.Class}; have you declared this activity in your AndroidManifest.xml?
at ui.utils.view.IntentUtilsKt.startNewActivityForResult(IntentUtils.kt:19)
at ui.main.hamburgerMenu.HamburgerMenuActivity.onClick(HamburgerMenuActivity.kt:117)
问题出现在重复的::class.java
调用中:
错误:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java) //duplicated ::class.java
startActivityForResult(intent, requestCode)
}
右:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity) // removed ::class.java
startActivityForResult(intent, requestCode)
}