Activities are the building blocks of Android apps. Each Activity creates a screen with UI. Having many Activities can increase memory use because each one holds its own UI and state. Switching between Activities involves system work to start and stop them, which can cause small delays and affect smoothness if overused. Properly managing Activity lifecycle helps keep battery use low and frame rates smooth, aiming for 60fps for good user experience.
Activity concept in Android Kotlin - Build, Publish & Deploy
- Reuse Activities when possible instead of creating many new ones.
- Keep UI simple and avoid heavy work on the main thread during Activity startup.
- Use asynchronous loading for data to prevent UI blocking.
- Manage lifecycle events to release resources when Activity is paused or stopped.
- Consider using Fragments or Jetpack Navigation to reduce Activity switches.
Activities themselves add minimal size to the app bundle since they are mostly code. However, each Activity may load different layouts and resources, increasing overall size. More Activities can increase app startup time if the launcher Activity loads many resources. Keeping layouts lightweight and sharing resources helps keep the app size and startup time small.
Android uses Activities as screens, while iOS uses ViewControllers. Android Activities have a complex lifecycle with states like created, started, resumed, paused, stopped, and destroyed. iOS ViewControllers have a simpler lifecycle. Android requires explicit management of Activity lifecycle to avoid memory leaks and performance issues. iOS handles some lifecycle events automatically. Navigation patterns differ: Android often uses Activities and Fragments, iOS uses ViewControllers and Navigation Controllers.
- Ensure Activities do not crash or freeze during startup to meet stability guidelines.
- Follow Android Material Design for UI consistency in Activities.
- Respect user privacy and permissions when Activities request sensitive data.
- Optimize Activity launch time to avoid poor user experience flagged by store reviews.
- Sign your APK or AAB properly before publishing.
Possible issues include heavy work on the main thread during Activity startup, loading large resources synchronously, or creating too many Activities unnecessarily. Optimizing by moving work off the main thread and simplifying the UI can help reduce load time.
Practice
Activity in an Android app?Solution
Step 1: Understand Activity purpose
An Activity is designed to show a screen where users interact.Step 2: Compare options
Only It represents a single screen with a user interface. correctly describes an Activity as a UI screen.Final Answer:
It represents a single screen with a user interface. -> Option DQuick Check:
Activity = Screen UI [OK]
- Confusing Activity with data storage
- Thinking Activity runs without UI
- Mixing Activity with background services
Solution
Step 1: Identify lifecycle methods
onCreate() is called when the Activity is first created to set up UI.Step 2: Eliminate other methods
onStart() and onResume() happen later; onPause() is for pausing.Final Answer:
onCreate() -> Option CQuick Check:
UI setup = onCreate() [OK]
- Choosing onStart() or onResume() for UI setup
- Confusing onPause() as setup method
- Not knowing Activity lifecycle order
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}Solution
Step 1: Analyze onCreate method
It calls super.onCreate and sets content view to activity_main layout.Step 2: Understand setContentView effect
This loads and displays the UI from activity_main.xml on screen.Final Answer:
The screen shows the layout defined in activity_main.xml. -> Option AQuick Check:
setContentView loads layout = screen shows layout [OK]
- Assuming onCreate does nothing
- Thinking app crashes without error
- Confusing blank screen with missing layout
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
super.onCreate(savedInstanceState)
}
}Solution
Step 1: Check order of calls in onCreate
super.onCreate() must be called before setContentView to initialize Activity properly.Step 2: Identify mistake
Here, setContentView is called first, which is incorrect and can cause issues.Final Answer:
setContentView called before super.onCreate() -> Option AQuick Check:
super.onCreate() first, then setContentView [OK]
- Calling setContentView before super.onCreate()
- Forgetting to override onCreate
- Not calling super.onCreate() at all
DetailActivity from MainActivity. Which code snippet correctly starts the new Activity?Solution
Step 1: Understand Intent creation
Intent requires context (this) and target Activity class (DetailActivity::class.java).Step 2: Check startActivity usage
startActivity(intent) correctly starts the new Activity with the Intent.Final Answer:
val intent = Intent(this, DetailActivity::class.java) startActivity(intent) -> Option BQuick Check:
Intent(this, TargetClass) + startActivity(intent) [OK]
- Omitting context in Intent constructor
- Calling startActivity without Intent
- Using wrong syntax for Intent creation
Ready to prove it?
Pass the test and this pattern moves unknown
