You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Compose导航图的composable代码块中以Activity作为ViewModelStoreOwner获取Activity作用域ViewModel的方法及泄漏风险疑问

How to Use Activity as ViewModelStoreOwner in a Jetpack Compose NavGraph Composable

First, let's walk through how to retrieve the Activity instance within your composable route:

You can access the current context via LocalContext.current, then safely cast it to your app's specific Activity type (a base Activity works too if you have one). It’s smart to add a type check to avoid runtime crashes:

composable(JetnewsDestinations.INTERESTS_ROUTE) {
    val context = LocalContext.current
    val activity = context as? AppCompatActivity // Replace with your actual Activity class
    activity?.let {
        val activityScopedViewModel: MyActivityScopedViewModel = viewModel(
            viewModelStoreOwner = it,
            factory = MyViewModelFactory(appContainer.someRepository)
        )
        InterestsRoute(
            interestsViewModel = interestsViewModel,
            activityScopedViewModel = activityScopedViewModel,
            isExpandedScreen = isExpandedScreen,
            openDrawer = openDrawer
        )
    }
}

For reusability, you could even wrap this logic in a helper composable:

@Composable
fun getCurrentActivity(): AppCompatActivity? {
    val context = LocalContext.current
    return when (context) {
        is AppCompatActivity -> context
        is ContextWrapper -> context.baseContext as? AppCompatActivity
        else -> null
    }
}

Is This Feasible?

Absolutely! As you pointed out, both Activity and Fragment implement ViewModelStoreOwner, so passing the Activity as the viewModelStoreOwner parameter is fully supported by the Jetpack ViewModel library. This scopes your ViewModel to the Activity’s lifecycle—meaning it will persist as long as the Activity is alive, even when navigating between different composables or screens. This is perfect for app-wide state that needs to be shared across multiple destinations, like user session data or app settings.

Will This Cause Memory Leaks?

The short answer: Not if you do it correctly.

Here’s the breakdown:

  • When you pass the Activity as the viewModelStoreOwner, the ViewModel library only uses it to access the ViewModelStore—it does not retain a strong reference to the Activity in the ViewModel itself.
  • Memory leaks only become a risk if you explicitly pass the Activity into your ViewModel (e.g., via the factory) and hold onto it without a weak reference. If your ViewModel doesn’t need direct access to the Activity, you’re completely safe.
  • If you do need context in the ViewModel, use the Application context (which is lifecycle-independent) instead of the Activity context. If you must use the Activity, wrap it in a WeakReference to ensure it can be garbage collected when the Activity is destroyed.

Remember: ViewModels should never hold strong references to UI components like Activities or Composables. Stick to using the Activity solely as the viewModelStoreOwner, and you won’t run into memory leak issues.


内容的提问来源于stack exchange,提问作者TomR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 17:19:09