Question
Start a New Activity with Intent in Kotlin Android
Question
How can I start a second Activity in a Kotlin Android app? The following code produces this error when creating the Intent:
Please specify constructor invocation; classifier
Page2does not have a companion object
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buTestUpdateText2(view: View) {
val changePage = Intent(this, Page2)
// Error: Please specify constructor invocation;
// classifier 'Page2' does not have a companion object
startActivity(changePage)
}
}
What should be changed to start Page2 correctly?
Short Answer
You will learn how Android uses an explicit Intent to open another screen, why Kotlin requires a class reference rather than a class name, and how to ensure the destination Activity is configured correctly.
Concept
An Android Activity represents a screen, such as a login screen, settings screen, or product-details screen. To move from one Activity to another, create an explicit Intent.
An explicit Intent identifies the exact destination Activity class:
Intent(currentContext, DestinationActivity::class.java)
In the original code, Page2 is only the name of a Kotlin class. Intent needs Java's runtime class representation, written in Kotlin as Page2::class.java.
This distinction matters because Android must know exactly which Activity component it should create and display. The Intent carries that destination information to Android's Activity system, and startActivity() asks the system to open it.
Mental Model
Think of an Intent as an addressed envelope.
- The current Activity (
this) is the return address and application context. - The destination Activity class (
Page2::class.java) is the recipient's address. startActivity(intent)hands the envelope to Android's delivery system.
Writing only Page2 is like writing the name of a building without supplying its actual address format. Page2::class.java gives Android the destination type it needs.
Syntax and Examples
The standard Kotlin syntax for opening another Activity is:
val intent = Intent(this, DestinationActivity::class.java)
startActivity(intent)
For this example, use Page2::class.java:
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buTestUpdateText2(view: View) {
val changePage = Intent(this, Page2::class.java)
startActivity(changePage)
}
}
The destination must be an Activity class:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class Page2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_page2)
}
}
::class creates a Kotlin class reference. Adding .java converts it to the Class object expected by the Android Intent constructor.
Step by Step Execution
Consider this click handler:
fun openPage2(view: View) {
val intent = Intent(this, Page2::class.java)
startActivity(intent)
}
Execution proceeds as follows:
- The user taps the view connected to
openPage2. thisrefers to the currentMainActivityinstance.Page2::class.javaidentifies thePage2Activity class.Intent(this, Page2::class.java)creates an explicit navigation request.startActivity(intent)sends the request to Android.- Android creates or resumes
Page2and calls its lifecycle methods, includingonCreate()when a new instance is needed. - The
Page2layout becomes visible.
When the user presses the system Back button, Android usually finishes Page2 and returns to MainActivity.
Real World Use Cases
Activities are often used for navigation between larger, independent screens, including:
- Opening a settings screen from a profile page.
- Moving from a sign-in screen to a main dashboard.
- Opening a product detail screen after a user selects an item.
- Launching a checkout or payment flow.
- Opening a separate camera, map, or document-preview Activity.
An Intent can also carry small pieces of data to the next screen:
val intent = Intent(this, Page2::class.java)
intent.putExtra("USER_ID", 42)
startActivity(intent)
Page2 can read that value from intent in its onCreate() method.
Real Codebase Usage
In real Android projects, explicit Intents are commonly kept close to UI event handling, while the destination receives validated input.
A typical sender pattern is:
fun openProfile(userId: String) {
if (userId.isBlank()) return
val intent = Intent(this, ProfileActivity::class.java)
.putExtra(ProfileActivity.EXTRA_USER_ID, userId)
startActivity(intent)
}
A destination Activity commonly defines keys as constants so that the sender and receiver use the same key:
class ProfileActivity : AppCompatActivity() {
companion object {
const val EXTRA_USER_ID = "extra_user_id"
}
}
For larger apps, developers may add a factory function in the destination's companion object to centralize Intent creation:
class ProfileActivity : AppCompatActivity() {
companion object {
const val EXTRA_USER_ID = "extra_user_id"
fun newIntent(context: Context, userId: String): Intent {
return Intent(context, ProfileActivity::class.java)
.putExtra(EXTRA_USER_ID, userId)
}
}
}
Then the caller uses startActivity(ProfileActivity.newIntent(this, userId)). This avoids repeating extra keys and makes navigation safer to update.
Common Mistakes
Passing the class name instead of a class reference
Incorrect:
val intent = Intent(this, Page2)
Page2 is a class declaration, not the Class value the constructor expects.
Correct:
val intent = Intent(this, Page2::class.java)
Instantiating an Activity yourself
Avoid this:
val page = Page2()
Android manages Activity creation and lifecycle callbacks. Create an Intent and call startActivity() instead.
Forgetting to make the destination an Activity
This will not work as a destination if Page2 is an ordinary Kotlin class:
class Page2
It should extend an Android Activity type, for example:
class Page2 : AppCompatActivity()
Missing the manifest declaration
An Activity normally must be declared in AndroidManifest.xml, especially if it was not created using Android Studio's Activity template:
<application ...>
<activity android:name=".Page2" />
<activity android:name=".MainActivity" android:exported="true">
<!-- launcher intent filter -->
</activity>
</application>
If it is missing, the app can compile but fail at runtime with an Activity-not-found error.
Using the wrong context
Inside an Activity, this is appropriate. In a Fragment, use requireContext() or often call the Fragment's startActivity():
startActivity(Intent(requireContext(), Page2::class.java))
Do not use an uninitialized or unrelated context.
Comparisons
| Approach | Meaning | When to use it |
|---|---|---|
Intent(this, Page2::class.java) | Opens one known Activity | Normal in-app screen navigation |
Intent(this, Page2) | Invalid for this constructor | Never; it is missing ::class.java |
Page2() | Directly creates a Kotlin object | Not for Activities; Android must create Activities |
| Explicit Intent | Names a specific component class | Opening a screen in your app |
| Implicit Intent | Describes an action, such as viewing a URL | Letting Android select a compatible app |
An implicit Intent example:
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://developer.android.com"))
startActivity(intent)
Unlike an explicit Intent, this does not name a specific Activity class. Android finds an installed app that can handle the action.
Cheat Sheet
// Open an Activity from another Activity
startActivity(Intent(this, Page2::class.java))
// Create the Intent first
val intent = Intent(this, Page2::class.java)
startActivity(intent)
// Send data
val intent = Intent(this, Page2::class.java)
intent.putExtra("KEY", "value")
startActivity(intent)
// Read data in the destination Activity
val value = intent.getStringExtra("KEY")
Rules:
- Use
DestinationActivity::class.java, not justDestinationActivity. - The destination must extend
ActivityorAppCompatActivity. - Let Android create Activities; do not call an Activity constructor directly.
- Declare manually created Activities in
AndroidManifest.xml. - Use
thisinside an Activity andrequireContext()inside a Fragment.
FAQ
Why does Kotlin require ::class.java when starting an Activity?
The Intent constructor expects a Java Class object for its destination. Page2::class.java supplies that object.
Can I use Page2::class without .java?
Not with the standard Intent(context, class) constructor. It expects Class<*>, so use Page2::class.java.
Does Page2 need to extend AppCompatActivity?
It must extend an Android Activity class. AppCompatActivity is a common choice because it provides compatibility features and works with AndroidX.
Why does the app crash after I fix the Kotlin error?
Check that Page2 is declared in AndroidManifest.xml. A missing declaration can cause Android to throw an Activity-not-found exception.
How do I open an Activity from a Fragment?
Use a valid Fragment context:
startActivity(Intent(requireContext(), Page2::class.java))
How do I pass information to the next Activity?
Add extras with putExtra() before calling startActivity(), then read them from the destination Activity's intent.
Should every Android screen be an Activity?
Not necessarily. Many modern apps use one Activity with multiple Fragments or Jetpack Compose navigation. Explicit Intents remain the standard way to launch another Activity.
Mini Project
Description
Build a two-screen app in which the first screen opens a greeting screen and sends the user's name through an explicit Intent. This demonstrates correct Activity navigation and basic Intent extras.
Goal
Open Page2 from MainActivity and display a name that was sent from the first screen.
Requirements
Create MainActivity with a button that opens Page2.
Create Page2 as an AppCompatActivity.
Use Page2::class.java as the Intent destination.
Send a name value using an Intent extra.
Display the received name on the second screen.
Declare Page2 in the application manifest.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Add a Maven Repository URL in Gradle Kotlin DSL (build.gradle.kts)
Learn how to add Maven Central and a custom Maven repository URL in Gradle Kotlin DSL using build.gradle.kts syntax.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.