推荐的方式来获得砂浆屏幕内的活动?

我正在使用Mortar和Flow来驱动我的应用程序。 如果我有以下视图:

public class MyView extends LinearLayout {
  @Inject MyScreen.Presenter presenter;
  private EditText someText;

  public MyView(Context context) {
    super(context);
    Mortar.inject(context, this);
  }

  @Override protected void onFinishInflate() {
    super.onFinishInflate();

    presenter.takeView(this);
  }

  @Override protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    presenter.dropView(this);
  }
 }

并在下面的屏幕上:

@Layout(R.layout.my_view)
public class MyScreen implements Blueprint {
  @Override public String getMortarScopeName() {
    return getClass().getName();
  }

  @Override public Object getDaggerModule() {
    return new Module();
  }

  @dagger.Module(injects = MyView.class, addsTo = MyActivity.Module.class)
  public class Module {
  }

  @Singleton
  public class Presenter extends ViewPresenter<MyView> {
    private final SomeAsyncService service;

    @Inject
    Presenter() { }

    @Override public void onLoad(Bundle savedInstanceState) {
    }

    @Override public void onSave(Bundle outState) {
    }

  }
}

现在我想访问正在使用此屏幕和视图的活动。 我将有权访问一些Activity方法,如:

getWindow()
finish()
getLayoutInflater()
getFragmentManager()

etc

我试图在屏幕中投射一个像这样的背景:

Activity activity = (Activity)getView.getContext;

但是,这当然会引发以下异常:

java.lang.ClassCastException: mortar.MortarContextWrapper cannot be cast to 
android.app.Activity

如何获得该活动? 谢谢。


不确定,但Mortar不推荐直接使用活动实例?

这个凡人样本是有帮助的。 所以,我执行如下。

class WindowOwner extends Presenter<WindowOwner.Activity> {

    interface Activity {
        void addFragsToWindow(int flags);
    }

    public static class Config() {
        final private int flags;
        Config(int flags) { this.flags = flags; }
        public int getFlags() { return this.flags; }
    }

    private Config config;

    public void setConfig(Config config) {
        this.config = config;
        update();
    }

    private void update() {
         WindowOwner.Activity activity = getView();
         if (activity != null) {
              activity.addFlagsToWindow(config.getFlags());
         }
    }

    @dagger.Module(injects = {}, library = true)
    public static class WindowOwnerModule {
         @Provides
         @Singleton
         WindowOwner provideWindowOwnerPersenter() { return new WindowOwner(); }
    }
}

你定义一个WindowOwner.Module ,并将这个类包含到你的模块中。

@Module(injects = {}, library = true)
public class AndroidModule {

    // WindowOwner provider
    @Provides
    @Singleton
    SystemBarOwner provideWindowOwnerPersenter() {
        return new WindowOwner();
    } 
}
@Module(
        includes = {
            AndroidModule.class, // Includes your modules
        },
        injects = {
               MainApplication.class
        },
        library = true
)
public final class MainModule {

    private final MainApplication mApplication;

    public MainModule(MainApplication application) {
        mApplication = application;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return mApplication;
    }
}

并且你像下面一样实施Activivty。

 class AwesomeActivity extends Activity implements WindowOwner.Activity {
      @Inject WindowOwner windowOwner;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          Mortar.inject(this, this);
          this.windowOwner.takeView(this);
      }

      @Override
      void addFragsToWindow(int flags) {
          getWindow().addFlags(flags);
      }
 }

您可以通过WindowOwner使用

@Layout(R.layout.awesome_view)
public class AwesomeScreen implements Blueprint {

    @Override
    public String getMortarScopeName() {
        return getClass().getName();
    }

    @Override
    public Object getDaggerModule() {
        return new Module(getViewState());
    }

    @dagger.Module(
            injects = {
                    AwesomeView.class // inject your view
            },
            addsTo = MainModule.class,
            library = true
    )
    public static class Module {
    }

    @Singleton
    public static class Presenter extends ViewPresenter<AwesomeView> {

         private final WindowOwner windowOwner;

         @Inject
         Presenter(WindowOwner windowOwner) {
             this.windowOwner = systemBarOwner;
         }

         @Override
         protected void onEnterScope(MortarScope scope) {
             super.onEnterScope(scope);

             // Update window flags via WindowOwner.Config
             this.windowOwner.setConfig(new WindowOwner.Config(YOUR_FLAGS));
         }
    }
}
链接地址: http://www.djcxy.com/p/82997.html

上一篇: Recommended way to get Activity inside Mortar screen?

下一篇: Umbraco https rewrite rule causes an infinite loop