天天看點

java inject Android,java – Dagger 2.11:使用@ContributesAndroidInjector時的Local Singleton

Component Application

/ \

Subcomponent DrawerActivity Subcomponent SubActivity

|

Subcomponent DrawerFragment

在SubActivity和DrawerActivity中,我使用标記為@Singleton的相同存儲庫執行個體.

@Singleton

@Component(modules = {

AndroidInjectionModule.class,

AppModule.class,

ActivityBuilderModule.class

})

public interface AppComponent {

@Component.Builder

interface Builder{

@BindsInstance Builder application(Application application);

AppComponent build();

}

void inject(App app);

}

@Module

public abstract class ActivityBuilderModule {

@PerActivity

@ContributesAndroidInjector(modules = {DrawerActivityModule.class,

FragmentBuilderModule.class})

abstract DrawerActivity bindDrawerActivity();

@PerActivity

@ContributesAndroidInjector(modules = {DrawerActivityModule.class})

abstract SubActivity bindSubActivity();

}

@Module

public abstract class FragmentBuilderModule {

@PerFragment

@ContributesAndroidInjector(modules = DrawerFragmentModule.class)

abstract DrawerFragment provideDrawerFragment();

}

@Singleton

public class Repository{

private SomeClass mSomeClass;

@Inject

public VehicleRepositoryImpl(SomeClass someClass) {

mSomeClass = someClass;

}

}

public class App extends Application implements HasActivityInjector{

@Inject

DispatchingAndroidInjector dispatchingAndroidInjector;

@Override

public void onCreate() {

super.onCreate();

if (BuildConfig.DEBUG) {

Timber.plant(new Timber.DebugTree());

}

AppComponent component = DaggerAppComponent.builder().application(this)

.build();

component.inject(this);

}

@Override

public AndroidInjector activityInjector() {

return dispatchingAndroidInjector;

}

}

public class DrawerActivity extends AppCompatActivity implements HasSupportFragmentInjector{

@Inject

DispatchingAndroidInjector fragmentDispatchingAndroidInjector;

@Override

public AndroidInjector supportFragmentInjector() {

return fragmentDispatchingAndroidInjector;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

AndroidInjection.inject(this);

super.onCreate(savedInstanceState);

}

}

public class DrawerFragment extends Fragment {

@Inject

ViewModelFactory mViewModelFactory; //repository gets injected into factory

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

AndroidSupportInjection.inject(this);

super.onCreate(savedInstanceState);

}

}

public class SubActivity extends AppCompatActivity{

@Inject

Repository mRepository;

@Override

protected void onCreate(Bundle savedInstanceState) {

AndroidInjection.inject(this);

super.onCreate(savedInstanceState);

}

}

我現在需要添加使用者管理.這需要LoginActivity.我不希望存儲庫在LoginActivity中可用.是以我想為DrawerActivity / Subactivity / DrawerFragement建立一個本地單例範圍“UserScope”

Component Application

/ \

@UserScope @LoginScope

Subcomponent DrawerActivity SubComponent LoginActivity

| \

Subcomponent DrawerFragment Subcomponent SubActivity

如何使用新的@ContributesAndroidInjector注釋實作此目的?

解決方法:

我在這個回購中解決了我的問題:

非常感謝它的貢獻者!

更新1:添加了代碼示例.

該圖提供了代碼示例的粗略概念.

Component Application

/ \

@UserScope @LoginScope

Subcomponent UserComponent SubComponent LoginActivity

| \

Subcomponent DrawerActivity Subcomponent SubActivity

|

SubComponent DrawerFragment

代碼示例(如果缺少某些内容請在評論中告訴我):

1.匕首設定

AppComponent是Dagger圖的根:

@Singleton

@Component(modules = {

AndroidSupportInjectionModule.class,

AppModule.class,

AppBindingModule.class

})

public interface AppComponent extends AndroidInjector {

@Component.Builder

interface Builder{

@BindsInstance Builder application(Application application);

AppComponent build();

}

void inject(MyApp app);

UserComponent.Builder userBuilder();

UserManager getUserManager();

}

綁定其子元件的子產品:

@Module(subcomponents = UserComponent.class)

public abstract class AppBindingModule {

@ContributesAndroidInjector(modules = LoginModule.class)

@LoginScope

abstract LoginActivity loginActivity();

}

UserComponent儲存僅在使用者登入時使用的類的執行個體.UserModule.class中提供的所有類在其他子元件(如活動和片段元件)中可用作“LocalSingletons”.

@UserScope

@Subcomponent(modules = {

UserBindingModule.class,

UserModule.class,

AndroidSupportInjectionModule.class

})

public interface UserComponent extends AndroidInjector {

void inject(UserManager userManager);

@Subcomponent.Builder

interface Builder{

UserComponent build();

}

}

UserBindingModule定義哪些活動子元件屬于UserComponent.

@Module

public abstract class UserBindingModule {

@ContributesAndroidInjector(modules = {DrawerBindingModule.class, AndroidSupportInjectionModule.class})

abstract DrawerActivity bindDrawerActivity();

@ContributesAndroidInjector

abstract SubActivity bindSubActivity();

}

DrawerBindingModule定義哪些片段子元件屬于DrawerActivityComponent.

@Module

public abstract class DrawerBindingModule {

@DrawerFragmentScope

@ContributesAndroidInjector(modules = DrawerFragmentModule.class)

abstract DrawerFragment provideDrawerFragment();

}

UserManager處理使用者登入/登出以及所有進一步的活動注入.

@Singleton

public class UserManager implements HasActivityInjector {

private final UserComponent.Builder userComponentBuilder;

@Inject

DispatchingAndroidInjector activityInjector;

private UserComponent userComponent;

@Inject

public UserManager(UserComponent.Builder builder) {

this.userComponentBuilder = builder;

}

public void logIn(){

createUserSession();

}

private void createUserSession() {

userComponent = userComponentBuilder.build();

userComponent.inject(this);

}

public boolean isLoggedIn() {

return userComponent != null;

}

public void logOut() {

userComponent = null;

}

@Override

public AndroidInjector activityInjector() {

return activityInjector;

}

}

2. App Activity Fragment類

public class MyApp extends Application implements HasActivityInjector{

@Inject

DispatchingAndroidInjector dispatchingAndroidInjector;

@Inject

UserManager mUserManager;

@Override

public void onCreate() {

super.onCreate();

if (BuildConfig.DEBUG) {

Timber.plant(new Timber.DebugTree());

}

AppComponent component = DaggerAppComponent.builder().application(this)

.build();

component.inject(this);

}

@Override

public AndroidInjector activityInjector() {

return mUserManager.activityInjector();

}

}

public class LoginActivity extends AppCompatActivity {

Intent mOpenDrawerActivity;

private ActivityLoginBinding binding;

@Inject

UserManager mUserManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

AndroidInjection.inject(this);

super.onCreate(savedInstanceState);

...

}

void openDrawerActivity(){

mUserManager.logIn();

mOpenDrawerActivity = new Intent(this, DrawerActivity.class);

startActivity(mOpenDrawerActivity);

finish();

}

}

public class DrawerActivity extends BaseUserActivity implements HasSupportFragmentInjector{

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

...

}

private void onLogout(){

logoutUser();

}

}

public abstract class BaseUserActivity extends BaseActivity {

@Inject

UserManager userManager;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (!userManager.isLoggedIn()) {

finishView();

}

}

@Override

protected void androidInject() {

AndroidInjection.inject(this);

}

protected void logoutUser() {

userManager.logOut();

finishView();

}

}

public abstract class BaseActivity extends AppCompatActivity implements HasSupportFragmentInjector {

@Inject

DispatchingAndroidInjector injector;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

androidInject();

super.onCreate(savedInstanceState);

}

protected void androidInject() {

AndroidInjection.inject(this);

}

public void finishView() {

startActivity(new Intent(this, LoginActivity.class));

finish();

}

@Override

public AndroidInjector supportFragmentInjector() {

return injector;

}

}

标簽:java,android,dagger-2

來源: https://codeday.me/bug/20190622/1263693.html