推荐在Mortar屏幕内获取Activity的方法?

时间:2014-12-22 18:07:02

标签: android mortar square-flow

我正在使用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。我可以访问一些Activity方法,如:

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

etc

我试图在我的屏幕中投射这样的上下文:

Activity activity = (Activity)getView.getContext;

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

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

如何进行该活动?谢谢。

1 个答案:

答案 0 :(得分:4)

不确定,但不建议Mortar直接使用活动实例?

This mortal sample很有帮助。 所以,我实现如下。

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));
         }
    }
}