Intent Activity Utils,未在清单中声明 - Kotlin,Android
Intent Activity Utils , not declarated in Manifest - Kotlin, Android
我正在尝试实现 Intent Activity Utils 作为 Kotlin 中的扩展函数。他们将从其他 Activity/Fragment 开始活动。请向我建议最好的方法()。
我试过这样解决那个问题:
在 Activity 中,我调用了这样的扩展函数:
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
}
IntentUtils.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)
}
此外,Activity 在清单中声明 - Kotlin,Android
<activity
android:name=".ui.recharge.RechargePrepaidActivity"
android:configChanges="locale|orientation|screenSize"
android:screenOrientation="portrait" />
这 activity 正常启动:
startNewActivity(MyProfileActivity::class.java) // use extend func
startActivity(Intent(this, MyProfileActivity::class.java)) // use inherited Activity func
但是当我尝试创建相同的函数来通过请求代码为结果启动 activity 时它不起作用,当我使用继承的 Activity 函数 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)
}
我正在尝试实现 Intent Activity Utils 作为 Kotlin 中的扩展函数。他们将从其他 Activity/Fragment 开始活动。请向我建议最好的方法()。
我试过这样解决那个问题:
在 Activity 中,我调用了这样的扩展函数:
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
}
IntentUtils.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)
}
此外,Activity 在清单中声明 - Kotlin,Android
<activity
android:name=".ui.recharge.RechargePrepaidActivity"
android:configChanges="locale|orientation|screenSize"
android:screenOrientation="portrait" />
这 activity 正常启动:
startNewActivity(MyProfileActivity::class.java) // use extend func
startActivity(Intent(this, MyProfileActivity::class.java)) // use inherited Activity func
但是当我尝试创建相同的函数来通过请求代码为结果启动 activity 时它不起作用,当我使用继承的 Activity 函数 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)
}