我对Dagger概念并不陌生,我在某种程度上已经了解了我的理解,但是在注入类方面一直存在问题。我经历了很多教程和示例代码,但是当我不得不使用一个组件的多个模块时,最终出现了一个错误,并且最终导致共享优先模块没有被注入。无法理解实际的错误或我犯的错误,需要一些帮助。
我的组件类:
@Singleton
@Component(modules = {VehicleModule.class, AppPreference.class})
public interface AppComponent {
// void injectPreference(MainActivity activity);
void inject(MainActivity activity);
Vehicle provideVehicle();
}
我的共享首选项类别:
@Module
public class AppPreference {
private SharedPreferences preferences;
private SharedPreferences.Editor edit;
@ApplicationScope
@Provides
@Inject
public SharedPreferences getPreferences() {
return preferences;
}
public AppPreference(Context context) {
// preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences = context.getSharedPreferences(context.getString(R.string.app_name), MODE_PRIVATE);
edit = preferences.edit();
}
@Singleton
@Provides
public String setDataPref(String strKey, String strValue) {
edit.putString(strKey, strValue);
commitPreference();
return strKey;
}
@Singleton
@Provides
public String removeFromPreference(String strKey) {
edit.remove(strKey);
return strKey;
}
public void commitPreference()
{
edit.commit();
}
@Singleton
@Provides
public String getDataPref(String strKey) {
return preferences.getString(strKey, "");
}
@Singleton
@Provides
public boolean clear() {
edit.clear();
commitPreference();
return true;
}
}
我的应用程序类别:
public class AppInstance extends Application {
AppComponent component;
@Override
public void onCreate() {
super.onCreate();
component = DaggerAppComponent.builder().appPreference(new AppPreference(getApplicationContext())).build();
}
public AppComponent getComponent() {
return component;
}
}
最后我的活动:
public class MainActivity extends AppCompatActivity {
// @Inject
// AppPreference preference;
private AppComponent appComponent;
Vehicle vehicle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
appComponent = DaggerAppComponent.builder().vehicleModule(new VehicleModule()).build();
vehicle = appComponent.provideVehicle();
((AppInstance) getApplicationContext()).getComponent().inject(this);
}
}
此代码能够构建DaggerAppComponent,但是一旦我在Mainactivity中注入AppPreference后,它就不再起作用。
在注入首选项类时我做错了什么? 需要帮助。
答案 0 :(得分:1)
您需要在匕首Graph中提供SharedPreference类的实例,鉴于您的代码,您可以通过以下方式实现此目标,
AppModule.java
@Module
class AppModule{
@Provides
@Singleton
public SharedPreference providesSharedPreferences(application:Application){
return new AppPreference(application);
}
}
AppPreferences.java
public class AppPreference {
private SharedPreferences preferences;
private SharedPreferences.Editor edit;
public SharedPreferences getPreferences() {
return preferences;
}
public AppPreference(Context context) {
// preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences = context.getSharedPreferences(context.getString(R.string.app_name), MODE_PRIVATE);
edit = preferences.edit();
}
public String setDataPref(String strKey, String strValue) {
edit.putString(strKey, strValue);
commitPreference();
return strKey;
}
public String removeFromPreference(String strKey) {
edit.remove(strKey);
return strKey;
}
public void commitPreference()
{
edit.commit();
}
public String getDataPref(String strKey) {
return preferences.getString(strKey, "");
}
public boolean clear() {
edit.clear();
commitPreference();
return true;
}
}
AppComponent.java
@Singleton
@Component(modules = {VehicleModule.class, AppModule.class})
public interface AppComponent {
void inject(MainActivity activity);
Vehicle provideVehicle();
}
MainActivity.java
class MainActivity extends Activity{
@Inject
SharedPreference sharedPreference; //this is injected like this
}
答案 1 :(得分:1)
在您的情况下,我会将AppPreference设置为Singleton并在需要的地方@Inject。
首先,您的组件应如下所示:
@Singleton // Constraints this component to one-per-application or unscoped bindings.
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(MainActivity mainActivity);
//Exposed to sub-graphs
Context context();
AppPreference appPreference();
}
}
然后您的模块应如下所示:
@Module
public class ApplicationModule {
private final Application application;
public ApplicationModule(Application application) {
this.application = application;
}
private final Application application;
public ApplicationModule(Application application) {
this.application = application;
}
@Provides
@Singleton
Context provideApplicationContext() {
return application;
}
@Provides
@Singleton
AppPreference provideAppPreference() {
return new AppPreference(provideApplicationContext());
}
}
然后在Application类中初始化应用程序组件,同时保留对其的引用,以便以后使用:
public ApplicationComponent getComponent() {
return applicationComponent;
}
private void initializeInjector() {
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
像这样在其onCreate()上注入Main Activity之后:
MyApplication.get(this).getComponent().inject(this);
您最终可以使用:
@Inject
AppPreference preference;
答案 2 :(得分:1)
您误解了一些概念和注释。
您可以通过用@Inject
注释字段或构造函数来注入对象。如果是Android活动,则只能使用field方法。因此,您的MainActivity.class
应该看起来像这样:
public class MainActivity extends AppCompatActivity {
@Inject
SharedPreference preference;
@Inject
Vehicle vehicle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inject dependencies into MainActivity
((AppInstance) getApplicationContext()).getComponent().inject(this);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
您还不需要在AppPreferences模块中使用@Inject
批注,因为您正在提供依赖项,而不是在那里注入。
说到提供,每当要插入其返回类型时,将调用用@Provides
注释的方法。如果有多个提供者方法具有相同的返回类型,则必须使用@Named
批注或自定义限定符来区分它们。您在String
模块中有多个返回AppPreferences
的提供者方法,但是我认为它们没有正确地标记为提供者,它们看起来更像是对SharedPreferences
对象的一些操作。清理后,应该剩下这个模块了:
@Module
public class AppPreference {
private SharedPreferences preferences;
public AppPreference(Context context) {
preferences = context.getSharedPreferences(context.getString(R.string.app_name), MODE_PRIVATE);
}
@ApplicationScope
@Provides
public SharedPreferences getPreferences() {
return preferences;
}
}
您必须像在SharedPreferences
类中一样在组件中公开Vehicle
:
@Singleton
@Component(modules = {VehicleModule.class, AppPreference.class})
public interface AppComponent {
void inject(MainActivity activity);
SharedPreferences sharedPreferences();
Vehicle vehicle();
}
编辑:
如果您需要某种SharedPreferences
功能的包装器,则可以创建一个自定义类(既不是Dagger Component也不是Module),例如MyAppPreferences
:
public class MyAppPreferences {
private SharedPreferences preferences;
public MyAppPreferences(SharedPreferences preferences) {
this.preferences = preferences;
}
// put setDataPref, removeFromPref, etc. in here
}
并像这样注入它:
@Module
public class AppPreferencesModule {
private Context context;
public AppPreferencesModule(Context context) {
this.context = context;
}
// Dagger will inject the SharedPreferences object using the providePreferences() provider
@ApplicationScope
@Provides
public MyAppPreferences provideMyAppPreferences(SharedPreferences preferences) {
return new MyAppPreferences(preferences);
}
@ApplicationScope
@Provides
private SharedPreferences providePreferences() {
return context.getSharedPreferences(context.getString(R.string.app_name), MODE_PRIVATE);
}
}
@Singleton
@Component(modules = {VehicleModule.class, AppPreferencesModule.class})
public interface AppComponent {
void inject(MainActivity activity);
// expose MyAppPreferences instead of SharedPreferences
MyAppPreferences myAppPreferences();
Vehicle vehicle();
}
public class MainActivity extends AppCompatActivity {
// inject MyAppPreferences instead of SharedPreferences
@Inject
MyAppPreferences myAppPreferences;
...
}