我是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中注入AppPpreferences,它就不再起作用了。
我在注入偏好类时做错了什么? 需要帮助..
你弄错了几个概念和注释。
通过使用@Inject
注释字段或构造函数来注入对象。对于安卓活动,您只能使用字段方法。所以你的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
批注或自定义限定符来区分它们。您有多个提供程序方法在AppPreferences
模块中返回String
,但是我认为它们没有正确标记为提供程序,它们看起来更像是对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;
}
}
并且您必须像Vehicle
类一样在组件中公开SharedPreferences
:
@Singleton
@Component(modules = {VehicleModule.class, AppPreference.class})
public interface AppComponent {
void inject(MainActivity activity);
SharedPreferences sharedPreferences();
Vehicle vehicle();
}
编辑:如果你想要某种SharedPreferences
功能的包装器,你可以创建一个自定义类(既不是dagger组件也不是模块),例如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;
...
}
您需要将SharedPreference 类的实例提供给 dagger Graph,给定您的代码,您可以通过以下方式实现这一点,
应用模块.java
@Module
class AppModule{
@Provides
@Singleton
public SharedPreference providesSharedPreferences(application:Application){
return new AppPreference(application);
}
}
应用首选项.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;
}
}
应用组件.java
@Singleton
@Component(modules = {VehicleModule.class, AppModule.class})
public interface AppComponent {
void inject(MainActivity activity);
Vehicle provideVehicle();
}
主活动.java
class MainActivity extends Activity{
@Inject
SharedPreference sharedPreference; //this is injected like this
}
在您的情况下,我会将AppPpreferences设置为单例,并将其@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());
}
}
然后在应用程序类中初始化应用程序组件,同时保留对它的引用,以便以后可以使用它:
public ApplicationComponent getComponent() {
return applicationComponent;
}
private void initializeInjector() {
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
在 onCreate() 上注入主活动后,如下所示:
MyApplication.get(this).getComponent().inject(this);
您最终可以使用:
@Inject
AppPreference preference;