MainActivity
TheMainActivity class is the primary entry point for the TechSales Android application, extending AppCompatActivity and implementing modern Android UI patterns.
Full Source Code
Location:~/workspace/source/TechSales/app/src/main/java/com/teamtech/techsales/MainActivity.java
Class Structure
Package and Imports
Import Breakdown
Import Breakdown
- android.os.Bundle: State management for activity lifecycle
- androidx.activity.EdgeToEdge: Modern immersive display API
- androidx.appcompat.app.AppCompatActivity: Base activity class with backwards compatibility
- androidx.core.graphics.Insets: Represents window inset dimensions
- androidx.core.view.ViewCompat: View compatibility utilities
- androidx.core.view.WindowInsetsCompat: Backwards-compatible window insets
onCreate() Method
TheonCreate() method initializes the activity and sets up the UI. Let’s break down each component:
1. Super Call
savedInstanceState parameter allows restoration of previous state if the activity is being recreated.
2. EdgeToEdge Enable
This is called before
setContentView() to ensure the window configuration is set up correctly before inflating the layout.What EdgeToEdge.enable() Does
- Visual Effect
- Technical Details
- Benefits
Enables the app to draw content behind the system bars (status bar and navigation bar), creating an immersive, full-screen experience.Before EdgeToEdge:After EdgeToEdge:
3. Content View Inflation
activity_main.xml and sets it as the activity’s content view. This establishes the UI hierarchy for the activity.
Location reference: MainActivity.java:17
4. Window Insets Handling
Step-by-Step Breakdown
Step-by-Step Breakdown
Step 1: Find the root viewRetrieves the root ConstraintLayout from the inflated layout.Step 2: Set the listenerRegisters a callback that executes whenever window insets change (e.g., keyboard appears, system bars change).Step 3: Extract system bar insetsGets the dimensions of system UI elements (status bar, navigation bar).Step 4: Apply paddingAdds padding to prevent content from being drawn under system bars.Step 5: Return insetsPasses insets to child views for further processing.
Insets Visualization
Lifecycle Methods
Currently, MainActivity only overridesonCreate(). The activity lifecycle follows the standard Android pattern:
Additional lifecycle methods like
onPause(), onStop(), and onDestroy() can be added as needed for resource management, state saving, or cleanup operations.Key Concepts
AppCompatActivity
- Backwards compatibility with older Android versions
- Material Design component support
- ActionBar/Toolbar support (even though we use NoActionBar)
- Fragment management utilities
Lambda Expression
The insets listener uses a lambda expression for concise code:OnApplyWindowInsetsListener interface.
Common Modifications
Adding Custom Toolbar
Adding Custom Toolbar
Selective Insets Application
Selective Insets Application
Handling Keyboard Insets
Handling Keyboard Insets
Best Practices
-
Always call EdgeToEdge.enable() before setContentView()
- Ensures proper window configuration
-
Handle window insets for all root views
- Prevents content from being obscured by system UI
-
Return insets from the listener
- Allows child views to also process insets
-
Use ViewCompat for backwards compatibility
- Ensures code works on older Android versions
-
Test on devices with different screen configurations
- Different notch sizes, navigation bar styles, etc.
Related Resources
Layouts
View the activity_main.xml layout
Overview
Components architecture overview