2014-12-22 44 views
7

我正在使用砂漿和流動來驅動我的應用程序。如果我有以下觀點:推薦的方式來獲得砂漿屏幕內的活動?

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

    } 
} 

現在我想可以訪問到使用此屏幕和查看活動。我想有一些活性的方法如訪問:

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

etc 

我試圖蒙上了上下文這樣我的屏幕裏:當然

Activity activity = (Activity)getView.getContext; 

但是,這將引發以下異常:

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

如何獲得該活動?謝謝。

回答

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

使用謝謝你,但我覺得這個例子中是行不通的。我試圖讓它在最近兩天工作,但它不起作用。你能否提供更多的細節? – user4386126

+1

@ user4386126好的,我添加了一些資源。 希望你有幫助。 – eccyan

+0

是。謝謝! – user4386126