代码之家  ›  专栏  ›  技术社区  ›  picKit

如何在moxy演示程序android中获取上下文

  •  3
  • picKit  · 技术社区  · 7 年前

    如何从Moxy Presenter获取活动上下文? 乍一看很容易……:1。添加 Context getMvpActivity (); 进入之内 MvpView 接口并实现。 2。在演示者呼叫中 getViewState().getMvpActivity() .

    但是moxy不允许将非void方法添加到 MVPVIEW 接口。 请帮帮我。

    P.S.我需要演示者中的上下文来初始化应用程序组件( activity 是的参数 static 吸气剂)。

    谢谢。对不起,有些语法错误。

    2 回复  |  直到 7 年前
        1
  •  5
  •   senneco    7 年前

    正确的解决方案不是在演示者中使用活动上下文。因为,在活动重新创建的情况下,此上下文将泄漏(因为演示者将仍然活着)。您可以使用应用程序上下文。可以通过演示者的构造函数传递它。

        2
  •  0
  •   picKit    7 年前

    通过添加 Activity 上下文作为参数 onViewCreated() . 这样地:

    //presenter super class
    public void onViewCreated (Activity activity) {
        //init component here
        //this.component = ...
        injectPresenter ();
    }
    
    protected PresenterComponent getComponent () {
        return this.component;
    }
    
    protected abstract void injectPresenter ();
    
    
    
    //presenter child class
    @Override
    public void onViewCreated (Activity activity) {
        super.onViewCreated(this);
    }
    
    @Override
    protected void injectPresenter () {
        //you can name "inject" different ways
        //in your presenter component interface
        getComponent().inject(this);
    }
    
    
    
    //activity class
    @Override
    protected void onCreate () {
        //P.S.(for beginners) variable presenter is the object of class
        //which extends Presenter super class
        presenter.onViewCreated(this);
    }