Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger scopes in Android

Tags:

square

dagger

Jake Wharton's talk at Devoxx 2013, Architecting Android Applications with Dagger, talked about creating a Dagger scope for logged in users only. This sort of thing sounds really clean, and I want to do this in my applications.

The code that was discussed in the talk was something along the lines of:

public class LoggedInActivity extends Activity {

    @Inject User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logged_in);

        DaggerScopesApp app = (DaggerScopesApp) getApplication();
        app.getObjectGraph().plus(new UserModule("exampleusername")).inject(this);

        findViewById(R.id.do_something_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoggedInActivity.this, user.username + " : " +
                        user.someValue++, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

However, if the injected User is scoped as a @Singleton, then it's properties will disappear on config change (as the object graph is created in onCreate).

The solution is pretty simple, you can just do this "plus" operation once and store the new object graph somewhere else (maybe the application class), but I was wondering if this is a good approach? Can anyone from Square can provide any insight into what you do in your applications? Do you just not have singleton objects in the "logged-in" graph?

like image 455
Bradley Campbell Avatar asked Apr 07 '14 02:04

Bradley Campbell


People also ask

What are the Dagger scopes?

A scope is an annotations class with specific additional annotations: @Scope and @Retention. @Scope annotation is provided by Dagger library to define custom scopes. In our example, we create two scopes: @ActivityScope (for activities) and @FragmentScope (for fragments).

What is Dagger used for Android?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What are scopes in Dagger 2?

Dagger 2 provides @Scope as a mechanism to handle scoping. Scoping allows you to “preserve” the object instance and provide it as a “local singleton” for the duration of the scoped component. In the last tutorial, we discussed a special scope called @Singleton.

What is difference between Hilt and Dagger?

In Dagger, we create scope annotations such as ActivityScope, FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.


1 Answers

The solution is pretty simple, you can just do this "plus" operation once and store the new object graph somewhere else (maybe the application class), but I was wondering if this is a good approach?

Yep. The logged-in graph's lifecycle needs to live as long as the user is logged in and the process is around. Since the activity's lifecycle is extremely short, this isn't a good place for it.

I used it as an example to ease people into the concept using something they are familiar with.

Can anyone from Square can provide any insight into what you do in your applications?

All graphs that aren't tied to UI are managed by an Application class. Through this we are guaranteed that it's created once, created first, and only disappears if the process dies.

Anything UI-related (activity-scope graphs, fragment-scope graphs, etc.) is plussed on top of these as the UI comes and goes.

like image 89
Jake Wharton Avatar answered Nov 16 '22 06:11

Jake Wharton