home / skills / pluginagentmarketplace / custom-plugin-android / platform

platform skill

/skills/platform

This skill helps you master Android Activity, Fragment, Intent, and Service lifecycles to build robust, responsive apps.

npx playbooks add skill pluginagentmarketplace/custom-plugin-android --skill platform

Review the files below or copy the command above to add this skill to your agents.

Files (10)
SKILL.md
2.5 KB
---
name: platform
description: Android core components lifecycle, Activities, Fragments, Services, Intent system.
version: "2.0.0"
sasmp_version: "1.3.0"

# Agent Binding
bonded_agent: 02-platform
bond_type: PRIMARY_BOND

# Skill Configuration
atomic: true
single_responsibility: Android platform components & lifecycle

# Parameter Validation
parameters:
  component:
    type: string
    enum: [activity, fragment, service, intent, permission]
    required: false
  lifecycle_state:
    type: string
    required: false

# Retry Configuration
retry:
  max_attempts: 2
  backoff: exponential
  on_failure: return_lifecycle_diagram

# Observability
logging:
  level: info
  include: [query, component_type, response_time]
---

# Android Platform Skill

## Quick Start

### Activity Lifecycle
```kotlin
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    
    override fun onDestroy() {
        super.onDestroy()
        // Cleanup
    }
}
```

### Fragment Usage
```kotlin
class UserFragment : Fragment() {
    private val viewModel: UserViewModel by viewModels()
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.user.observe(viewLifecycleOwner) { user ->
            updateUI(user)
        }
    }
}
```

## Key Concepts

### Lifecycle Callbacks
- `onCreate()`: Initial setup
- `onStart()`: Become visible
- `onResume()`: Gain focus
- `onPause()`: Lose focus
- `onStop()`: Hidden
- `onDestroy()`: Final cleanup

### Fragment Lifecycle
Similar to Activity but with:
- `onAttach()`: Attached to activity
- `onDetach()`: Detached
- Fragment manager for transactions

### Intent System
```kotlin
// Explicit
startActivity(Intent(this, DetailActivity::class.java))

// Implicit
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com")))
```

### Services
- Started: `startService()`
- Bound: `bindService()`
- Foreground: Visible notification

## Best Practices

✅ Handle lifecycle properly
✅ Use ViewModel for state
✅ Unregister listeners
✅ Test configuration changes
✅ Respect process lifecycle

## Resources

- [Activity Lifecycle](https://developer.android.com/guide/components/activities)
- [Fragment Guide](https://developer.android.com/guide/fragments)
- [Service Documentation](https://developer.android.com/guide/components/services)

Overview

This skill exposes core Android platform concepts: component lifecycles, activity and fragment patterns, services, and the Intent system. It helps developers reason about state transitions, lifecycle callbacks, and correct integration of ViewModel, listeners, and background work. The goal is practical guidance for building robust, lifecycle-aware apps.

How this skill works

The skill inspects and explains what to do at each lifecycle callback (onCreate, onStart, onResume, onPause, onStop, onDestroy) and maps equivalent Fragment callbacks (onAttach, onViewCreated, onDetach). It demonstrates explicit and implicit Intents, describes started, bound, and foreground services, and recommends where to place setup, cleanup, and long-lived work. It also highlights integration points for ViewModel and observer lifecycles to avoid leaks and data loss across configuration changes.

When to use it

  • Structuring Activities and Fragments to manage UI state and resources safely
  • Deciding between started, bound, or foreground Services for background work
  • Implementing explicit and implicit Intents for navigation and inter-app actions
  • Designing ViewModel-backed components to survive configuration changes
  • Unregistering listeners and cleaning up resources on lifecycle transitions

Best practices

  • Initialize UI and resources in onCreate/onViewCreated; release them in onDestroy/onDetach
  • Use ViewModel and LiveData/Flow for state so UI controllers remain lightweight
  • Avoid heavy work on main thread; use WorkManager/Coroutines for background tasks
  • Unregister listeners in onPause/onStop to prevent leaks and wasted CPU
  • Test configuration changes and process death scenarios to ensure state restoration

Example use cases

  • Implementing a detail screen with explicit Intent navigation from a list Activity
  • Using a Fragment with a ViewModel to preserve form state across rotation
  • Running continuous location updates in a foreground Service with a persistent notification
  • Binding to a music playback service from multiple Activities for shared control
  • Launching an implicit Intent to open a URL or share content with other apps

FAQ

When should I use a Fragment instead of an Activity?

Use Fragments to compose reusable UI pieces within an Activity, enable multi-pane layouts, and share ViewModel state between related UI sections.

How do I avoid memory leaks with observers and listeners?

Register observers with lifecycle-aware owners (viewLifecycleOwner in Fragments) and unregister non-lifecycle listeners in onPause/onStop or onDestroy as appropriate.

When is a foreground Service required?

Use a foreground Service for user-noticeable long-running tasks (e.g., media playback, navigation) that must continue while the app is backgrounded; show a persistent notification.