gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:29 30 4
gpt4 key购买 nike

我正在使用 Mortar 和 Flow 来支持我的应用。如果我有以下 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) {
}

}
}

现在我想访问使用此屏幕和 View 的 Activity 。我想访问一些 Activity 方法,例如:

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

etc

我曾尝试在我的屏幕中转换这样的上下文:

Activity activity = (Activity)getView.getContext;

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

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

如何获得该 Activity ?谢谢。

最佳答案

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

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

关于android - 在 Mortar 屏幕内获取 Activity 的推荐方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27608108/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com