我正在尋找一種方法,使用Flow從屏幕返回結果,而不會丟失前一屏幕中的內容,類似於我在onActivityResult中所做的。例如,這裏是我創建新文檔的屏幕:砂漿和流動的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();
}
}
}
可能會出現這種情況,即堆棧被殺死並從Parcelable重新創建。在這種情況下'resultReceiver'可能會是'null'。所以我這樣做'((TakesResult)(flow.getBackstack()。reverseIterator()。next()。getScreen()))。onResult(t);' 另外我有一個情況,當結果應該是如果用戶按下後退按鈕,所以我在'onExitScope()' –
2015-01-29 14:23:10