4

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();
    }
  }
}
4

1 に答える 1

0

Popup と PopupPresenter はおそらく間違いでした。私が見たより良いテクニックは、「ダイアログ」画面がその結果を Flow バックスタックの前の画面オブジェクトの既知の一時フィールドに書き込むことです。一方では、これはかなりハックに思えます。一方、それは非常にシンプルで非常に読みやすいです。

于 2014-11-01T16:02:40.793 に答える