Issue
I am trying to achieve Intent Activity Utils as extended functions in Kotlin. They will start activities from other Activity/Fragment. Please suggest to me the best way to do that().
I tried to solve that problem like that:
In Activity I invoke extend functions like that :
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)
}
and also, the Activity is declared in the Manifest - Kotlin, Android
<activity
android:name=".ui.recharge.RechargePrepaidActivity"
android:configChanges="locale|orientation|screenSize"
android:screenOrientation="portrait" />
This starts activity normally:
startNewActivity(MyProfileActivity::class.java) // use extend func
startActivity(Intent(this, MyProfileActivity::class.java)) // use inherited Activity func
But when I try to create the same function for starting activity for result via request code it doesn't work, when I use inherited Activity function startActivityForResult()
it's ok
// 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)
The strange here is that when I use my IntentUtils.startNewActivityForResult()
app crashes with
ActivityNotFoundException
, but on normal use of Fragment.startActivityForResult()
it doesn't have this behaviour:
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)
Solution
The problem was in the duplicated ::class.java
invocation :
WRONG:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java) //duplicated ::class.java
startActivityForResult(intent, requestCode)
}
RIGHT:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity) // removed ::class.java
startActivityForResult(intent, requestCode)
}
Answered By - Red Coder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.