简体   繁体   English

如何将依赖项注入和匕首用于onCreate()方法中已创建的视图

[英]How to use dependency injection with dagger for views that are already created in onCreate() method

I am new to dependency injection. 我是依赖注入的新手。 I wanted to avoid using static methods. 我想避免使用静态方法。 For simplicity i am showing the code that is only relevant. 为简单起见,我只显示了相关的代码。

Here is my code: 这是我的代码:

public class MainActivity extends AppCompatActivity {
     private ImageView mFavorites_fab;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mFavorites_fab = (ImageView) findViewById(R.id.favorites_fab);
     }

     void vibrateFab(Context context) {   //Function to be called using DI
         Log.v(TAG, "vibrate fab" +context);

         Animation vibrateAnimation = AnimationUtils.loadAnimation(context, R.anim.vibrate);
         mFavorites_fab.setAnimation(vibrateAnimation);
     }

I wanted to call vibrateFab function from another class using Dependency injection 我想使用依赖注入从另一个类中调用vibrateFab函数

This is how I am trying to call. 这就是我要打电话的方式。

Module: 模块:

@Module
public class MainActivity_Module {

    @Provides
    @ActivityScope
    MainActivity providesMainActivity() {
        return new MainActivity();
    }
}

Component: 零件:

@Component(modules = MainActivity_Module.class)
@ActivityScope
public interface MainActivity_Component {
    void inject(Nearby_Viewpager_Stops nearby_viewpager_stops);
} 

Another class: 另一类:

public class Nearby_Viewpager_Stops extends Fragment {
    @Inject
    MainActivity mainActivity;
    Context mContext;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;

        MainActivity_Component component = DaggerMainActivity_Component.builder().mainActivity_Module(new MainActivity_Module()).build();
        component.inject(Nearby_Viewpager_Stops.this);
        mMainActivity.vibrateFab(mContext);

    }

Now the problem is: In this line mFavorites_fab is null as I am using new instance of Main Activity. 现在的问题是:在这一行中,由于我正在使用Main Activity的新实例,因此mFavorites_fab为null。

 mFavorites_fab.setAnimation(vibrateAnimation);

How to overcome this? 如何克服呢?

Interesting. 有趣。 I don't think Dagger can give you dependices on .class it would need an object. 我认为Dagger不能给您依赖.class的东西,它需要一个对象。 The way to get this is * in your Application create an Application Component. 实现此目的的方法是*在您的应用程序中创建一个应用程序组件。 ApplicationComponent would have a module provider. ApplicationComponent将具有一个模块提供程序。 providing vibrateFab; 提供vibrateFab;

From your activity get access to the AcitivityComponet and then say inject into the class Now you can use ActivityInjector method to avoid some bipolate code 从您的活动中访问AcitivityComponet,然后说“注入到类中”现在您可以使用ActivityInjector方法来避免一些双向代码

I would strongly recomemend following example http://www.vogella.com/tutorials/Dagger/article.html#exercise_androiddagger 我强烈推荐以下示例http://www.vogella.com/tutorials/Dagger/article.html#exercise_androiddagger

Happy coding!! 编码愉快!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM