我正在寻找一种使用 Flow 从屏幕返回结果的方法不会丢失在上一个屏幕中构建的内容,类似于我对 onActivityResult 所做的。例如,这是我正在创建新文档的屏幕:
@Layout(R.layout.new_document_view)
class NewDocumentScreen implements Blueprint {
// Imagine some Blueprint boiler plate here
static class Presenter implements ViewPresenter<NewDocumentView> {
private final Flow flow;
private Document newDocument = new Document();
@Inject Presenter(Flow flow) { this.flow = flow; }
@Override protected void onLoad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
NewDocumentView view = getView();
if (view == null) return;
view.bindTo(newDocument); // immediately reflect view changes in document
}
// Imagine this is called by pressing a button in NewDocumentView
public void chooseDocumentAuthor() {
// What I want here is to navigate to the chooser screen, make my choice and
// then return to this screen having set the author on the document.
flow.goTo(new ChooseDocumentAuthorScreen());
}
}
}
我该怎么做?我一直在试验 Popup和 PopupPresenter但是关于这些的信息不多,而且我不相信这是正确的方法,因为选择器本身就是一个屏幕。
更新——潜在的解决方案
基于以下来自 @rjrjr 的回答我做了以下似乎工作正常:
TakesResult.java
public interface TakesResult<T> {
// Called when receiving a result
void onResult(T result);
}
新文档屏幕.java
@Layout(R.layout.new_document_view)
class NewDocumentScreen implements Blueprint, TakesResult<Author> {
private Document newDocument = new Document();
@Override public void onResult(Author result) {
newDocument.setAuthor(result);
}
// Imagine some Blueprint boiler plate here
@dagger.Module(injects = NewDocumentView.class, addsTo = MainScreen.Module.class)
class Module {
@Provides Document provideDocument() { return newDocument; }
@Provides NewDocumentScreen provideScreen() { return this; }
}
static class Presenter implements ViewPresenter<NewDocumentView> {
private final Flow flow;
private final NewDocumentScreen screen
private final Document newDocument;
@Inject Presenter(Flow flow, NewDocumentScreen screen, Document newDocument) {
this.flow = flow;
this.screen = screen;
this.newDocument = newDocument;
}
@Override
protected void onLoad(Bundle savedInstanceState) {
// Stuff to update view
}
// Imagine this is called by the view
public void chooseDocumentAuthor() {
// Since screen TakesResult we send it to the ChooseAuthorScreen
flow.goTo(new ChooseDocumentAuthorScreen(screen));
}
}
}
ChooseAuthorScreen.java
@Layout(R.layout.choose_author_view)
class ChooseAuthorScreen implements Blueprint {
private final TakesResult<Author> resultReceiver;
ChooseAuthorScreen(TakesResult<Author> resultReceiver) {
this.resultReceiver = resultReceiver;
}
// Imagine some Blueprint boiler plate here
@dagger.Module(injects = ChooseAuthorView.class, addsTo = MainScreen.Module.class)
class Module {
@Provides TakesResult<Author> provideResultReceiver() { return resultReceiver; }
}
static class Presenter implements ViewPresenter<ChooseAuthorView> {
private final Flow flow;
private final TakesResult<Author> resultReceiver;
@Inject Presenter(Flow flow, TakesResult<Author> resultReceiver) {
this.flow = flow;
this.resultReceiver = resultReceiver;
}
// Imagine this is called by the view
public void chooseAuthor(Author author) {
resultReceiver.onResult(author);
flow.goBack();
}
}
}
最佳答案
Popup 和 PopupPresenter 可能是一个错误。我见过的一种更好的技术是让“对话”屏幕将其结果写入 Flow 后台堆栈中前一个屏幕对象上众所周知的 transient 字段。一方面,这看起来很老套。另一方面,它非常简单且可读性强。
关于java - 等同于 Mortar 和 Flow 的 onActivityResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26643649/