2014-10-30 10 views
4

我正在尋找一種方法,使用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()); 
    } 
    } 
} 

我該怎麼做?我一直在試驗PopupPopupPresenter,但這些信息並不多,我不相信這是正確的選擇,因爲選擇器本身就是一個屏幕。

更新–潛在的解決方案

基於下面的答案從@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

可能會出現這種情況,即堆棧被殺死並從Parcelable重新創建。在這種情況下'resultReceiver'可能會是'null'。所以我這樣做'((TakesResult )(flow.getBackstack()。reverseIterator()。next()。getScreen()))。onResult(t);' 另外我有一個情況,當結果應該是如果用戶按下後退按鈕,所以我在'onExitScope()' – 2015-01-29 14:23:10

回答

0

彈出和PopupPresenter很可能是錯誤的。我所見過的一種更好的技術是讓「對話框」屏幕將其結果寫入Flow backstack中前一個屏幕對象上衆所周知的瞬態場。一方面,這看起來很哈克。另一方面,它非常簡單,非常易讀。

+0

有趣,謝謝。我在GitHub的一個問題上看到你曾經說過這樣的話。我會給你一個去,讓你知道我如何繼續。 – kuhnza 2014-11-02 03:05:47

+0

剛剛爲上面的問題添加了一個可能的解決方案,我接受了你的建議,但增加了一個界面來正式確定兩者之間的關係。思考? – kuhnza 2014-11-02 03:52:00