Introduction
An Activity is like a single screen in an app. It shows the user what to see and lets them interact with the app.
An Activity is like a single screen in an app. It shows the user what to see and lets them interact with the app.
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
onCreate() is where you set up the screen.
setContentView() links the screen to a layout file.
class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) } }
override fun onStart() {
super.onStart()
// Code to run when activity becomes visible
}This simple Activity shows a text message on the screen.
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val textView = TextView(this) textView.text = "Hello from Activity!" setContentView(textView) } }
Every screen in Android is an Activity or uses one.
Activities have a lifecycle: onCreate, onStart, onResume, onPause, onStop, onDestroy.
Use the AndroidManifest.xml to declare your Activity.
An Activity is a screen in your app.
Use onCreate() to set up the screen.
Activities manage user interaction and app flow.
Activity in an Android app?class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
super.onCreate(savedInstanceState)
}
}DetailActivity from MainActivity. Which code snippet correctly starts the new Activity?Pass the test and this pattern moves unknown