Java Interopt Android Intent


#1

I’m having a rather bizarre issue at the moment.

I have an activity written in Kotlin.

The issue comes when I try to start a new activity from my kotlin activity.

  --  startActivity(Intent(this, SearchResultsActivity::class.java))

If I try to launch an activity written in Kotlin it just launches a blank activity.
Unless I launch the current activity again.

If I try to launch an activity written in Java is works as expected.

The activity I am trying to launch IS in the manifest, and when I take it out of the manifest
I get the error that it isn’t in the manifest.

I am debugging and the onCreate in my super super simple kotlin activity I am launching is never being called.

It looks like android is just launching a sort of DEFAULT or blank activity instead.

EDIT: Actually if I change the launch activity to any other activity besides my StartActivity in kotlin onCreate doesn’t get called and the activity is blank.

This is probobly the most confusing issue I’ve had in the past 2 years of professional dev.


#2

You might try flipping to use Anko. The syntax then looks like

startActivity(intentFor<SearchResultsActivity>())

Or even:

startActivity<SearchResultsActivity>()

You use the first variant, when you want to load up the intent with extras, flags, and whatnot. Without Anko, the above link suggests you say

val intent = Intent(this, javaClass<SomeOtherActivity>())
intent.putExtra("id", 5)
intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)

Leading you to the odd question of what exactly is the difference between javaClass<Foo>() and Foo::class.java. I don’t have a clear understanding of the answer to that question.

What “worked for me” when I had to deal with this, before I started using Anko, was something like so:

val activityIntent = Intent(context, PermissionActivity::class.java)
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(activityIntent)

So, and this is now in wild-assed-guess territory, you’ll notice that startActivity is really a method on a Context of some sort. You might want to investigate exactly what context you’re using, or should be using, to make this happen.


#3

Leading you to the odd question of what exactly is the difference between javaClass() and Foo::class.java. I don’t have a clear understanding of the answer to that question.

That is easy. At one time they were the same. javaClass() is old syntax that became deprecated and is now no longer valid.


#4

I encounter the same problem today. Most probably you overrides the onCreate with two parameters onCreate(savedInstanceState: Bundle, persistentState: PersistableBundle) instead of the original onCreate
so your onCreate was never called. This might be due to an IDE auto-complete issue.