The rise of single activity applications
The evolution of Android Development has seen a significant change from multi -activity applications towards single activity architecture. In this approach, instead of creating a new activity for each screen, developers produce the entire application in the same activity.
This fundamental change raises important questions about when the context and life cycle management works when only one activity works as the basis of the entire application.
Single activity apps with pieces: first step
Prior to entering the jet pack composing scene, developers created single activity applications using pieces and XML layouts. In this model, the application announces only one activity, and it Activity
Will serve as a container for different pieces representing different screens:
// The one Activity declared in the manifest
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)// The container where Fragments will be shown
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, HomeFragment())
.commit()
}
}
}
Despite being just one Activity
For, for, for,. The context system is necessary. Single Activity
Created by Android System, still provides Context
Bridge for operating system. All pieces share this activity Context
For their UI operations, including losing layouts and access to resources. The pieces have their own life cycle ways, but they are closely tied and integrated by the parent’s activity life cycle.
Jetpack Composes: Next evolution
Jet Pack Composes The need for pieces and XML layout is fully eliminated by eliminating the architecture of single activity. With composes, developer explain through UI Composingable functions Instead of XML files:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme {
// Navigation happens inside Compose
MainNavHost() // This replaces Fragment transactions
}
}
}
}
However, The basic context system does not disappear – It just turns into how it accesses and uses it. In the composing application, there is still at least one Activity
Announced in the manifesto. These Activity
Calls setContent()
Instead of setContentView()
Establishing composes as a UI framework. Activity
Continues to work as a bridge between the application and the Android system, and provides Context
It is important for many operations.
The context flows in composing applications
When using composes in a single activity architecture, the flow of context is important but less visible. Activity context passes through a system of structural And was made available through LocalContext
Composites:
@Composable
fun ScreenWithSystemAccess() {
// Getting the Activity's Context in Compose
val context = LocalContext.currentColumn {
Button(onClick = {
// Using Context to show a toast
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
}) {
Text("Show Toast")
}
// Using Context to access resources
Text(text = context.getString(R.string.welcome_message))
}
}
Many composing functions do not require direct context to build basic UI elements, which makes the dependence of the context less clear. However, operations, such as toast show, access to resources, or the use of system services, still require context.
This context is derived from activity, maintaining the basic Android model where the context works with the communication channel with the Android system.
Life Cycle Management in Compos
Composite applications need to see a life cycle to see how composing interacts with your own life cycle concepts Activity
Life Cycle
Introduces the concept of composes Structure life cycle – UI elements enter the structure when they appear and leave the structure when they disappear. This compose is handled through the run time and the activity varies from the life cycle.
However, Activity Life Cycle is still significantly significant. The activity created by OS still receives the events of the life cycle as onCreate
For, for, for,. onResume
For, for, for,. onPause
And onDestroy
. These events can be observed by composes LocalLifecycleOwner
:
@Composable
fun ScreenWithLifecycleAwareness() {
// This gives us access to the Lifecycle from the Activity
val lifecycleOwner = LocalLifecycleOwner.current// We can observe lifecycle events from the Activity
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_RESUME -> {
// Do something when Activity resumes
}
Lifecycle.Event.ON_PAUSE -> {
// Do something when Activity pauses
}
}
}
// Register our observer to the lifecycle
lifecycleOwner.lifecycle.addObserver(observer)
// Clean up when this composable leaves composition
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
}
This means that while composing its own form of life cycle, he does not change the activity life cycle – Instead, both work together. This activity receives life cycle events from the system and can send these events to a composing composer by the loungeeal owner.
The relationship between the context, the life cycle, and the state becomes particularly clear when we see how we handle the reserves of the composing state. Offers two main perspectives to collect the state from composing flow: collectAsState
And collectAsStateWithLifecycle
:
@Composable
fun UserProfileScreen(viewModel: ProfileViewModel) {
// Basic collection - continues in background
val basicState = viewModel.basicStateFlow.collectAsState().value// Lifecycle-aware collection - pauses in background
val lifecycleAwareState = viewModel.importantStateFlow
.collectAsStateWithLifecycle().value
// Use the states in UI...
}
The difference between these two functions depends on the awareness of life. Basic collectAsState
Whenever the compose is in the compound mixture, the function accumulates with a flow, regardless of whether the application is in the foreground or background. On the contrary, collectAsStateWithLifecycle
Respects the life cycle of activity – when the activity is in the background and when the activity returns to the forefront, it accumulates.
Life cycle familiar combination is possible because the composes have access to the activity life cycle by the loucific owner. Function activity Life Cycle Events uses to organize an effective manner to collect flow, shows how Activity context also continues to affect the behavior of application in a composing UI.
Continuous importance of context
Even when Android Development is developed by a more modern view, such as Single Activity architecture and writing UI, The basic context system is very important. The context still works for the necessary bridge between your application and Android system, providing access to resources, system services and life cycle programs.
What has changed is how we interact with this system. In multi -activity applications, each activity provided its context. In single activity applications with pieces, an activity context is shared in several pieces. In composing applications, the activity context is available by composing composable environmental items such as local concourse and folklorial owners.
This evolution shows how Android’s basic architecture will adopt its basic principles. Understanding the context and lifetime role in these modern points, helps developers make applications that work effectively with the Android system.Regardless of which UI framework or architecture they choose.