0

I created Activity, ViewModel, Modules for Toothpick and Authenticator.

@Singleton
public class GetSmsViewModel {

    @Inject Application app;

    @Inject Authenticator authenticator;
...
}


public class GetSmsActivity extends AppCompatActivity {

    private Scope appScope;
    @Inject GetSmsViewModel mGetSmsViewModel;
...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        appScope = Toothpick.openScope(getApplication());
        appScope.installModules(new DIModule(getApplication()), new DataModule());

        super.onCreate(savedInstanceState);

        Toothpick.inject(this, appScope);

   ...
   }
}

public class DIModule extends Module {
    public MagicDeliveryMainModule(Application application) {
        bind(GetSmsViewModel.class).toInstance(new GetSmsViewModel());
        bind(Application.class).toInstance(application);
        bind(Authenticator.class).toInstance(new Authenticator());
    }
}

In the documentation for the Toothpick is written : "If Toothpick creates an instance, it will always inject its dependencies." , but after Toothpick.inject(this, appScope);

mGetSmsViewModel.app == null and mGetSmsViewModel.authenticator == null . And after Toothpick.inject(mGetSmsViewModel, appScope); app and authenticator fields become injected.

so it should be?

Kirill
  • 55
  • 7

1 Answers1

0

In you example Toothpick is not creating the instance, you are creating the instance and binding it.

For toothpick to create the instance, you need to bind to the target implementation class and provide an inject constructor.

shalafi
  • 3,926
  • 2
  • 23
  • 27