我正在寻找一种方法,使用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);
}
NewDocumentScreen.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();
}
}
}
答案 0 :(得分:0)
Popup和PopupPresenter可能是个错误。我见过的一种更好的技术是让“对话框”屏幕将其结果写入Flow Backstack中前一个屏幕对象上的一个众所周知的瞬态字段。一方面,这看起来非常hacky。另一方面,它非常简单,非常易读。