Junior — Middle
Can you explain the concept of Single Activity in the context of mobile app development?
sobes.tech AI
Answer from AI
The Single Activity concept in mobile development means that the application contains only one Activity, which serves as a container for various screens implemented through fragments or navigation components.
Advantages:
- Simplifies navigation and lifecycle management.
- Reduces overhead for creating and destroying Activities.
- Allows the use of modern navigation libraries (e.g., Navigation Component in Android).
Example: instead of launching a new Activity for each screen, the app switches fragments within a single Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Navigation between fragments
supportFragmentManager.beginTransaction()
.replace(R.id.container, HomeFragment())
.commit()
}
}
This simplifies architecture and improves user experience through smooth transitions and state preservation.