Activity x Compose Lifecycle: The Complete Visual Guide 🚀

 

👨‍💻 Activity Lifecycle


Activity.onCreate() <- Activity is created

↓

Activity.onStart() <- Preparing to become visible on screen

↓

Activity.onResume() <- Foreground state (interactive)

↓

Activity.onPause() <- Partially visible

↓

Activity.onStop() <- No longer visible

↓

Activity.onDestroy() <- Activity is destroyed

 

👨‍💻 Compose Lifecycle


[First Composition]
  - Composable is evaluated, Compose tree is built
  - LaunchedEffect -> Runs once after the commit
  - SideEffect -> Runs after every commit
  - DisposableEffect -> onDispose is called upon disposal

↓

[Recomposition]
  - UI is re-evaluated in response to state or data changes
  - Only the necessary parts are recomposed (efficient update)
  - SideEffect and DisposableEffect are also re-evaluated during recomposition

↓

[Dispose]
  - Depends on the ComposeView's disposal condition
  - DisposableEffect's onDispose is executed
  - Disposal timing is determined by the ViewCompositionStrategy

 

👨‍💻 Activity x Compose Lifecycle


Activity.onCreate() setContent { ... } <- Sets the ComposeView

↓

[First Composition]
  - Evaluates Composables and builds the UI
  - LaunchedEffect -> Runs once after commit
  - SideEffect -> Runs after each commit
  - DisposableEffect -> Defines onDispose

↓

Activity.onStart()

↓

Activity.onResume()

↓

[Recomposition]
  - Re-evaluates necessary parts in response to state changes
  - LaunchedEffect is not re-executed (only if its key changes)
  - SideEffect / DisposableEffect are re-evaluated

↓

Activity.onPause()

↓

(ComposeView is retained)
  - UI becomes partially obscured
  - State is maintained within the Composition

↓

Activity.onStop()

↓

[Preparing for Dispose (Detection)]
  - ViewCompositionStrategy monitors disposal conditions

↓

Activity.onDestroy()

↓

[Dispose]
  - ComposeView is destroyed
  - DisposableEffect.onDispose() is executed

This is the general flow.

Here are the notes for each item:

Activity.onCreate() A lifecycle method called when an Android app's Activity is created.

    setContent { ... }: Sets the UI using Jetpack Compose. This sets a ComposeView as the Activity's content view. The Compose lifecycle begins.

  • First Composition The Composable functions set in setContent are evaluated for the first time, and the UI is built.

LaunchedEffect: An Effect used for asynchronous processing. Runs only once after the first composition.

SideEffect: Runs after every composition commit (the timing when UI changes are applied).

DisposableEffect: An Effect used for resource cleanup. The logic defined in onDispose is executed when the Composition is disposed.

Activity.onStart() A lifecycle method called just before the Activity becomes visible to the user.

Activity.onResume() The Activity moves to the foreground and becomes fully interactive with the user.

  • Recomposition Only the necessary Composable functions are re-evaluated in response to changes in State.

LaunchedEffect: It is not re-executed during recomposition. However, it will be re-executed if its key changes.

SideEffect: It is re-evaluated on every recomposition.

DisposableEffect: It is re-evaluated on recomposition, and the cleanup logic (onDispose) from the old Effect is called.

Activity.onPause() The Activity enters a paused state and becomes partially obscured.

ComposeView Retention: The UI is not destroyed; it is retained. The state is also maintained within the Composition, allowing it to be reused upon re-display.

Activity.onStop() The Activity is no longer visible. A time to prepare for cleaning up state and resources.

Preparing for Dispose (Detection) ViewCompositionStrategy: A mechanism that monitors the conditions under which the ComposeView should be disposed. It triggers the Composition's disposal based on the View's lifecycle.

Activity.onDestroy() The timing when the Activity is completely destroyed.

  • Dispose The ComposeView is destroyed: The UI and state are completely released.
  • DisposableEffect.onDispose(): The resource cleanup logic is called.

 

👨‍💻 Summary

The Jetpack Compose and Activity lifecycles are closely linked, with specific processes occurring at each stage, from UI initialization (setContent) to disposal (Dispose).

It is especially important to understand the differences between effects like LaunchedEffect, SideEffect, and DisposableEffect, and to manage them appropriately.

Furthermore, by efficiently reusing the UI in response to Activity state changes (like onResume or onPause) and cleaning up resources as needed, you can achieve stable application behavior.


初回コンポーズは Activity.onStart() までに終わる

 

🤔 Activity x Compose のライフサイクル


Activity.onCreate()
  setContent { ... }   ← ComposeView をセット

    ↓

  [初回コンポーズ(First Composition)]
    ・Composable を評価し UI を構築
    ・LaunchedEffect → コミット後に一度実行
    ・SideEffect → 各コミットごとに実行
    ・DisposableEffect → onDispose 定義

    ↓

Activity.onStart()

    ↓

Activity.onResume()

    ↓

  [再コンポーズ(Recomposition)]  
    ・状態変更に応じて必要部分のみ再評価
    ・LaunchedEffect は再実行されない(Key 変更時のみ)
    ・SideEffect / DisposableEffect は再評価

    ↓

Activity.onPause()

    ↓

  (ComposeView は保持)
    ・UI は部分的に見えなくなる
    ・状態は Composition 内で維持

    ↓

Activity.onStop() 

    ↓

  [破棄準備(Dispose 検知)]
    ・ViewCompositionStrategy による破棄条件監視

    ↓

Activity.onDestroy()

    ↓

  [破棄(Dispose)]
    ・ComposeView が破棄される
    ・DisposableEffect.onDispose() 実行

 

🤔 まとめ

初回コンポーズは Activity.onStart() までに終わる。

👉 これだけでわかる!Activity × Compose のライフサイクル完全図解 🚀


Android Studio で Gemini と AI Assistant プラグインを使い、「自動生成された英語のコミットメッセージ」を日本語化する

本記事は、Android Studio 上で Gemini(モデル)と AI Assistant(プラグイン)を併用し、AI によって自動作成された英語のコミットメッセージを、チーム規約に沿った自然な日本語へ変換する実践手順をまとめたものです。

英語での自動草案は便利ですが、そのままだと意図やニュアンスが伝わりにくいことがあります。

ここでは「英語の自動生成を起点に、短い操作で日本語化」「テンプレ化して常に日本語で提案」の二段構えで対処します。

 

🧑🏻‍💻 Gemini:Prompt Library を編集し、「常に日本語で提案」されるようテンプレ化

毎回 “Translate to Japanese.” を書くのが手間なら、Gemini のプロンプトテンプレートに翻訳指示を埋め込み、英語で自動生成されるコミットでも出力は日本語に統一します。

手順

1. Android Studio の


設定 → Tools → Gemini → Prompt Library

2. Scope を IDE に設定
3. 以下を選択


Built-in Actions → 「Commit Message Generation」

4. プロンプト本文の末尾に次を追加して保存


Translate to Japanese.

効果

・ コミット時の提案が常に日本語で出力され、英語の自動草案を逐一翻訳する手間がなくなる
・ チームで設定を共有すれば、言語運用と文体の統一が容易
・ 既定の変数($SAMPLE_COMMIT_MESSAGES, $COMMIT_DIFF など)はそのまま活用可能

よりニュアンスを保ちたい場合は、翻訳行を次に差し替えるのも有効です。


Translate to natural Japanese, preserving engineering nuance and concision. Keep Conventional Commits types in English and code identifiers as-is.

 

🧑🏻‍💻 AI Assistant:英語の自動草案を、コミット直前に即座に日本語化

最短のワークフローは、コミットダイアログ右側の「AI Assistant」で英語の自動生成メッセージに対し、1 行の指示を追加して日本語化する方法です。

手順

1. コミットダイアログを開く
2. 右ペイン「AI Assistant」の「Prompt for generation」に次を追記する:


Translate to Japanese.

3. 生成結果をタイトル・本文に反映してコミット
4. 必要に応じて「Run advanced checks after a commit is done」を有効化し、生成後の検査を自動実行

ポイント

・ 自動生成の英語タイトル(例: feat: Add comments to MainViewModel)を、短く自然な日本語に置換
・ Conventional Commits の型(feat, fix など)は英字のまま保持し、意味合いは本文で補足
・ 冗長化を避ける既定ルールが効いて、端的な日本語に収まりやすい

英語の自動草案は背景を省きがちです。本文で「〜のため」を 1 行だけ補って理由を明示すると、レビュー時の誤解を減らせます。

 

🧑🏻‍💻 まとめ

・ Android Studio で Gemini と AI Assistant を併用すれば、AI が自動生成した英語のコミットメッセージを、短い操作で自然な日本語に変換できる
・ すぐ使うなら AI Assistant のプロンプトに “Translate to Japanese.” を追加
・ 恒久化するなら Gemini の Prompt Library で翻訳指示をテンプレ化
・ 運用の合言葉は「型は英字・サブジェクトは日本語・本文で背景と影響」。英語の自動草案の不足分は、日本語本文で補いましょう。


AIが書いたコードは静かにアプリを壊す — “動く”ことに満足した開発の行き着く先 —

 

🧑🏻‍💻 1. 「動くコード」を生み出すAIの魔法と、その落とし穴

最近の開発現場では、「AIにコードを書かせる」のが当たり前になってきた。
ChatGPTやGitHub Copilotに「○○を実装して」と伝えれば、数秒で動くコードを出してくれる。
それをコピーして貼り付ければ、ビルドも通り、アプリも動く。
——まるで魔法のようだ。

けれど、実はこの“動くコード”こそがアプリを静かに壊していく。
なぜなら、そのコードは「なぜ動くのか」を誰も理解していないからだ。
AIは文脈を「設計思想」ではなく「統計的パターン」で捉える。
だから動作優先で、構造や責務を無視したコードを出すことがある。

その結果、「とりあえず動いたからOK」というコードが少しずつ積み上がる。
数カ月後、いざ修正しようとしたとき、
「この処理、誰がどうしてこう書いたんだっけ?」
——誰も答えられない。
そんな“ブラックボックス化”が静かに進んでいく。

 

🧑🏻‍💻 2. 小さな歪みが積もって、アプリの寿命を縮める

AIが生成するコードは、局所的には正しい。
しかし全体で見れば、設計のバランスを壊していることが多い。

たとえばMVVM構造のアプリで、AIが「便利なショートカット」としてUI層から直接データベースにアクセスするようなコードを出す。
確かに動く。けれど、これは明確に“構造違反”だ。
一見問題ないように見えても、
こうした「小さな歪み」が何十箇所も積み重なると、アプリは急速に脆くなる。

特にAIは“最短距離”で解決しようとする。
テストやエラーハンドリングは省かれ、
依存ライブラリも無自覚に増える。
それでも最初は動くため、気づかない。
だが、ある日突然ビルドが通らなくなったり、
AndroidやiOSのバージョン更新で全体が壊れたりする。

原因を追っても、「どこから手をつければいいのか」が分からない。
AIが生み出した“つぎはぎ構造”が、まるで崩れかけた積み木のように、
どこを直しても全体がぐらつく。
——それが「AIがアプリの寿命を縮める」という現象の正体だ。

 

🧑🏻‍💻 3. AIと共存するために、設計を“守る人間”が必要になる

AIを完全に排除することは現実的ではない。
むしろ、うまく使えば開発効率は飛躍的に上がる。
問題は、「AIに書かせたあとをどう扱うか」だ。

たとえば、AIに出させたコードは必ずレビューする。
一人開発でも“レビュー時間”を意識的に取る。
「動くかどうか」ではなく、「構造的に正しいか」を基準に見る。
さらに、AIへの指示(プロンプト)も工夫する。
「なぜこの方法なのか」「設計上のリスクは?」と質問を加えるだけで、
AIの出力品質は大きく変わる。

AIはあくまで“補助輪”だ。
自転車を前に進ませることはできても、
どの道を走るべきかまでは決めてくれない。

これからのエンジニアは、
AIを“使う人”ではなく“指揮する人”になる必要がある。
AIの力で速く動く一方で、
人間が「構造を守るブレーキ役」を担う。
そのバランスこそが、アプリを長生きさせる秘訣だ。

 

🧑🏻‍💻 まとめ:AIは便利な酸素、でも吸いすぎると毒になる

AIは開発のスピードを何倍にも引き上げる。
しかし、構造を無視したまま使えば、
そのスピードでアプリの寿命を削り取る。

“動く”ことだけをゴールにしてしまうと、
“生き続ける”ための土台が壊れていく。

AIが生み出すコードは、確かに速く、便利で、魅力的だ。
でも、それを理解し、守り、磨き続けるのは人間の役割だ。
アプリの未来は、AIの性能ではなく、
それを正しく使う開発者の構造への愛情にかかっている。


JSON と JSON5 の違い

JSON と JSON5 は見た目は似ていますが、「どれくらい人間に優しいか」「どれくらい厳格か」で大きく違います

 

🧩 JSONとは

JavaScript Object Notation の略で、
データ交換フォーマットとして最も一般的なものです。

 

✅ 特徴

・厳格でシンプル
・仕様が固定されていて機械処理に最適
・ほとんどの言語・ツールが標準対応

 

📋 制約


{
  "name": "Mao",
  "age": 25,
  "languages": ["Swift", "Kotlin"]
}

・文字列は必ず ダブルクォート
・コメント不可
・末尾カンマ禁止
・キーもクォート必須
・数値表現 は10進数のみ

 

🌈 JSON5とは

JSON for Humans(人間に優しいJSON)で
JSONの上位互換として提案されたフォーマットです。

JavaScriptライクな書き方を許します。

 

✅ 特徴

・より柔軟で可読性が高い
・コメントや末尾カンマをサポート
・キーや文字列のクォート省略OK
・16進数や+∞などの数値表現が可能

 

📋 例


{
  // コメントOK
  name: 'Mao',
  age: 25,
  languages: ['Swift', 'Kotlin',], // 末尾カンマOK
  version: 0x1f, // 16進数OK
}

 

🤔 JSON と JSON5 の比較

JSON は機械向けの厳格フォーマット、JSON5 は「人間向け」に拡張されたフォーマットです。下の表はブログでそのまま使える比較表です。

項目 JSON JSON5
コメント 不可(コメントなし) 可(`//`、`/* */` が使える)
末尾カンマ 不可
キーのクォート 必須(ダブルクォート) 省略可(識別子としてのキーを許可)
文字列のクォート ダブルクォートのみ(`"`) シングルクォートまたはダブルクォート可(`'` / `"`)
数値表現 10進数のみ 10進数、16進数、`NaN`、`Infinity` などを許可
対応ツール 非常に多い(ほぼ標準) ライブラリが必要(サポートは限定的)
用途 機械間通信(API等)に最適 開発者向け設定ファイルや手で編集するデータに適する

 

🧑🏻‍💻 まとめ

- JSON:厳格で機械向け。安全・標準的。
- JSON5:柔軟で人間向け。コメントや末尾カンマOK。