4

我正在寻找一种方法来使用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());
    }
  }
}

我怎样才能做到这一点?我一直在尝试使用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));
    }
  }
}

选择AuthorScreen.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();
    }
  }
}
4

1 回答 1

0

Popup 和 PopupPresenter 可能是一个错误。我见过的一种更好的技术是让“对话框”屏幕将其结果写入 Flow backstack 中前一个屏幕对象上的一个众所周知的瞬态字段。一方面,这似乎很老套。另一方面,它非常简单且可读性强。

于 2014-11-01T16:02:40.793 回答