---
title: "Discussion on \"Using an Activity from a Hilt ViewModel\" | Hashnode"
description: "Discussion on \"Using an Activity from a Hilt ViewModel\". 👋 Hi, this is P.Y., I work as an Android Engineer at Block. This blog shares a bit of hackery to be able to access an activity instance within a Hilt ViewModel. If you come up with other interesting"
url: "https://hashnode.com/posts/using-an-activity-from-a-hilt-viewmodel/631914555abe8613b3d2f620"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Using an Activity from a Hilt ViewModel](https://blog.p-y.wtf/using-an-activity-from-a-hilt-viewmodel?utm_source=hashnode&utm_medium=feed)

👋 Hi, this is P.Y., I work as an Android Engineer at Block. This blog shares a bit of hackery to be able to access an activity instance within a Hilt ViewModel. If you come up with other interesting

Sep 7, 2022 · 5 min read · blog.p-y.wtf  
Updated Mar 26  
Read the article

-   [#android](https://hashnode.com/tag/android)
-   [#dagger-hilt](https://hashnode.com/tag/dagger-hilt)
-   [#dependency-injection](https://hashnode.com/tag/dependency-injection)
-   [#viewmodel](https://hashnode.com/tag/viewmodel)

## Written by

[Pierre-Yves Ricau](https://hashnode.com/@py)

@py · San Francisco

  
[View profile](https://hashnode.com/@py)

Deep dives

My pronouns are he / him. Android Engineer at Square / Block. A few things I enjoy: Publishing Open Source libraries (LeakCanary, Curtains, Radiography, square/logcat) Rollerblading Writing technical blogs (previously on dev.to/pyricau) Cooking Giving tech talks Reading sci-fi

## Responses 2

Markdown supported · ⌘↵ to post

[Eugen Martynov](https://hashnode.com/@emartynov)

[Sep 12, 2022](https://hashnode.com/posts/using-an-activity-from-a-hilt-viewmodel/631914555abe8613b3d2f620/comment/631f50df526d775aa7f65966)

My first thought was that the provider would implement the activity lifecycle callback. So I would just have one line to register it.

However, when you have multiple of them, maybe this is better so you can control the order of the calls or what is the intent?

[Pierre-Yves Ricau](https://hashnode.com/@py)

[Sep 12, 2022](https://hashnode.com/posts/using-an-activity-from-a-hilt-viewmodel/631914555abe8613b3d2f620/comment/631f50df526d775aa7f65966#reply-631f5c326c86d75accc7e627)

Yeah the general idea here is that this ensures you add an activity to a specific activity stack, rather than starting it from a random live activity or from the application context which isn't tied to a specific activity stack.

[florent guillemot](https://hashnode.com/@coreflodev)

[Sep 12, 2022](https://hashnode.com/posts/using-an-activity-from-a-hilt-viewmodel/631914555abe8613b3d2f620/comment/631eef1df73f4734bc05a4e4)

Hi Py,

I don't get your blog post to start an activity you don't have to have an activity right? a context is enough as it is possible to call context.startActivity

Thanks, Flo

[Eugen Martynov](https://hashnode.com/@emartynov)

[Sep 12, 2022](https://hashnode.com/posts/using-an-activity-from-a-hilt-viewmodel/631914555abe8613b3d2f620/comment/631eef1df73f4734bc05a4e4#reply-631f5079526d775aa7f65938)

Unfortunately, context is enough in limited cases.

[Pierre-Yves Ricau](https://hashnode.com/@py)

[Sep 12, 2022](https://hashnode.com/posts/using-an-activity-from-a-hilt-viewmodel/631914555abe8613b3d2f620/comment/631eef1df73f4734bc05a4e4#reply-631f5bf66c86d75accc7e5f8)

Starting an activity from the Application class isn't the same as starting it from an activity. When using the application class the activity manager doesn't know which activity stack the new activity should be added to.
