Owner of ActionBar in Mortar

In Mortar, I'm curious how people handle an ActionBar if it changes depending on the screen being displayed. Let's say, for example, that you want to change the title or actions of an ActionBar if a specific screen is displayed.

Enabling an activity seems like a terrible idea, and you want the presenter (or apparently I'm guessing) to be able to get the correct action bar after screen rotation.

Is there a particularly effective pattern here that I am missing? All the solutions I had but were more awkward than I hoped.

+3


source to share


1 answer


For activities, the MainActivity sample flow includes:

public boolean onCreateOptionsMenu(Menu menu) {
  ...
  Object screen = AppFlow.get(this).getBackstack().current().getScreen();
  ...

public void go(Backstack nextBackstack, Flow.Direction direction, Flow.Callback callback)
  Screen screen = (Screen) nextBackstack.current().getScreen();
  ...

      

Both methods know the screen to be displayed. Actions are defined in onCreateOptionsMenu (), header in go (). The problem is that it is all in MainActivity.

What you can do is create an ActionEnabledScreen interface using the method:

public boolean onCreateOptionsMenu(Menu menu);

      

And the TitleEnabledScreen interface with the method:



public void getTitle();

      

Your activity will look like this:

public boolean onCreateOptionsMenu(Menu menu) {
  ...
  Object screen = AppFlow.get(this).getBackstack().current().getScreen();
  if (screen instanceof ActionEnabledScreen) {
    return ((ActionEnabledScreen)screen).onCreateOptionsMenu(menu);
  }
  ...

public void go(Backstack nextBackstack, Flow.Direction direction, Flow.Callback callback)
  Screen screen = (Screen) nextBackstack.current().getScreen();
  if (screen instanceof TitleEnabledScreen) {
    setTitle(((TitleEnabledScreen)screen).getTitle());
  }
  ...

      

Please note that I have not run the above code, so it cannot compile / run, but I hope you see the idea. The goal is to avoid the MainActivity expanding linearly when you introduce new screens, and avoid any need to reference your screen classes directly in it.

Hope this helps!

+1


source







All Articles