问题描述
我用的砂浆和流量来驱动我的应用程序。如果我有以下观点:
I am using Mortar and Flow to power my app. If I have the following View:
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) {
}
}
}
现在我想可以访问它使用这种屏幕和查看活动。我想有机会获得一些活动方法,如:
Now I would like to get access to Activity which is using this Screen and View. I would to have access to some of the Activity methods like:
getWindow()
finish()
getLayoutInflater()
getFragmentManager()
etc
我试图蒙上了上下文这样我的屏幕里:
I have tried to cast a Context like this inside my Screen:
Activity activity = (Activity)getView.getContext;
但是,这当然会引发以下异常:
But this of course throws the following exception:
java.lang.ClassCastException: mortar.MortarContextWrapper cannot be cast to
android.app.Activity
如何获得这项活动?谢谢你。
How to get that activity? Thank you.
推荐答案
不肯定,但砂浆不建议直接使用活动实例?
Not for sure, but Mortar is not recommended to use activity instance directly ?
这个凡人样本是有帮助的。所以,我实现如下:
This mortal sample is helpful.So, I implemented as follows.
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
,包括这个类来你的模块是这样的。
You define a WindowOwner.Module
, and include of this class to your module like this.
@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喜欢如下:
And you implement Activivty like follows.
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使用
You can be used via 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));
}
}
}
这篇关于推荐的方式进去砂浆屏幕活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!