首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android Activity 跳轉基本用法與模組化封裝

     編輯:程式設計疑難解答

Activity 間跳轉核心 API:

// 顯式跳轉
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra("key", "value"); // 攜帶資料
startActivity(intent);

// 隱式跳轉 (啟動其他 App 組件)
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com"));
startActivity(intent);

大型專案模組化建議

  1. 按功能分包:
    com.app
      ├─ ui
      │   ├─ login (LoginActivity, LoginFragment, LoginViewModel)
      │   ├─ main  (MainActivity, HomeFragment, ...)
      │   └─ profile
      ├─ data (Repository, RoomDao, RetrofitService)
      ├─ domain (UseCase, Entity)
      └─ di   (Hilt/Koin Module)
  2. 跳轉封裝:建立 Navigator 單例或擴充函數,集中管理跳轉邏輯:
    object Navigator {
        fun startLogin(context: Context) {
            context.startActivity(Intent(context, LoginActivity::class.java))
        }
        fun startMain(context: Context, userId: String) {
            Intent(context, MainActivity::class.java).apply {
                putExtra("USER_ID", userId)
            }.also(context::startActivity)
        }
    }
  3. 動態模組:使用 Dynamic Feature Module (Play Feature Delivery) 按需下載功能模組,跳轉時檢查模組是否安裝 (SplitInstallManager)。

核心原則

「跳轉邏輯集中化、參數命名規範化、模組邊界清晰化。」
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved