gpt4 book ai didi

android - Mortar + Flow 与连接到 Activity 生命周期的第三方库

转载 作者:IT老高 更新时间:2023-10-28 23:32:20 26 4
gpt4 key购买 nike

一些第三方库在 Activity 生命周期中使用 Hook 来正常工作 - 例如,Facebook SDK (https://developers.facebook.com/docs/android/login-with-facebook/)。

我在弄清楚如何将此模型与单 Activity 流程 + 砂浆设置完全协调时遇到了一些麻烦。

例如,如果我想使用 Facebook 登录作为登录流(w/FlowView/FlowOwner)的一部分,但不是在 Activity 中,如果您需要该特定流的 Hook ,那么最聪明的方法是什么?在 onCreate、onResume、onPause、onDestroy、onSaveInstanceState、onActivityResult 等?

最干净的路径是什么并不是很明显 - 为每个生命周期 Activity 阶段创建一个 observable 并订阅它的流?如果您不小心,似乎这条路径很快就会转移到相同的 Android 生命周期。有没有更好的办法?

我喜欢单一 Activity 模型,如果可能的话,我真的希望一切都由流/迫击炮而不是 Activity 来管理。还是我在考虑这件事的方式从根本上使它变得比应有的更困难?

最佳答案

到目前为止,我们还不需要开始和停止,但确实有一些地方需要暂停和恢复。我们按照您的建议使用 ActivityPresenter,但避免使用任何类型的通用父类(super class)。相反,它公开了感兴趣的演示者可以选择加入的服务。这种连接需求是添加 onEnterScope(Scope) 方法的原因。这是代码。

首先,让 Activity 实现这个接口(interface):

/**
* Implemented by {@link android.app.Activity} instances whose pause / resume state
* is to be shared. The activity must call {@link PauseAndResumePresenter#activityPaused()}
* and {@link PauseAndResumePresenter#activityResumed()} at the obvious times.
*/
public interface PauseAndResumeActivity {
boolean isRunning();

MortarScope getMortarScope();
}

并让它注入(inject)演示者并进行适当的调用:

private boolean resumed;
@Inject PauseAndResumePresenter pauseNarcPresenter;

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pauseNarcPresenter.takeView(this);
}

@Override public boolean isRunning() {
return resumed;
}

@Override protected void onResume() {
super.onResume();
resumed = true;
pauseNarcPresenter.activityResumed();
}

@Override protected void onPause() {
resumed = false;
super.onPause();
pauseNarcPresenter.activityPaused();
}

@Override protected void onDestroy() {
pauseNarcPresenter.dropView(this);
super.onDestroy();
}

现在感兴趣的各方可以注入(inject)一个注册商接口(interface)来选择暂停和恢复通话,而无需继承任何内容。

/**
* Provides means to listen for {@link android.app.Activity#onPause()} and {@link
* android.app.Activity#onResume()}.
*/
public interface PauseAndResumeRegistrar {
/**
* <p>Registers a {@link PausesAndResumes} client for the duration of the given {@link
* MortarScope}. This method is debounced, redundant calls are safe.
*
* <p>Calls {@link PausesAndResumes#onResume()} immediately if the host {@link
* android.app.Activity} is currently running.
*/
void register(MortarScope scope, PausesAndResumes listener);

/** Returns {@code true} if called between resume and pause. {@code false} otherwise. */
boolean isRunning();
}

让客户端演示者实现这个接口(interface):

/**
* <p>Implemented by objects that need to know when the {@link android.app.Activity} pauses
* and resumes. Sign up for service via {@link PauseAndResumeRegistrar#register(PausesAndResumes)}.
*
* <p>Registered objects will also be subscribed to the {@link com.squareup.otto.OttoBus}
* only while the activity is running.
*/
public interface PausesAndResumes {
void onResume();

void onPause();
}

然后像这样连接起来。 (注意不需要注销。)

private final PauseAndResumeRegistrar pauseAndResumeRegistrar;

@Inject
public Presenter(PauseAndResumeRegistrar pauseAndResumeRegistrar) {
this.pauseAndResumeRegistrar = pauseAndResumeRegistrar;
}

@Override protected void onEnterScope(MortarScope scope) {
pauseAndResumeRegistrar.register(scope, this);
}

@Override public void onResume() {
}

@Override public void onPause() {
}

这是 Activity 注入(inject)的演示者,以使其全部工作。

/**
* Presenter to be registered by the {@link PauseAndResumeActivity}.
*/
public class PauseAndResumePresenter extends Presenter<PauseAndResumeActivity>
implements PauseAndResumeRegistrar {

private final Set<Registration> registrations = new HashSet<>();

PauseAndResumePresenter() {
}

@Override protected MortarScope extractScope(PauseAndResumeActivity view) {
return view.getMortarScope();
}

@Override public void onExitScope() {
registrations.clear();
}

@Override public void register(MortarScope scope, PausesAndResumes listener) {
Registration registration = new Registration(listener);
scope.register(registration);

boolean added = registrations.add(registration);
if (added && isRunning()) {
listener.onResume();
}
}

@Override public boolean isRunning() {
return getView() != null && getView().isRunning();
}

public void activityPaused() {
for (Registration registration : registrations) {
registration.registrant.onPause();
}
}

public void activityResumed() {
for (Registration registration : registrations) {
registration.registrant.onResume();
}
}

private class Registration implements Scoped {
final PausesAndResumes registrant;

private Registration(PausesAndResumes registrant) {
this.registrant = registrant;
}

@Override public void onEnterScope(MortarScope scope) {
}

@Override public void onExitScope() {
registrations.remove(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Registration that = (Registration) o;

return registrant.equals(that.registrant);
}

@Override
public int hashCode() {
return registrant.hashCode();
}
}
}

关于android - Mortar + Flow 与连接到 Activity 生命周期的第三方库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21927990/

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