Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling navigation (not really an issue) #2

Open
gervaisb opened this issue Jan 30, 2015 · 0 comments
Open

Handling navigation (not really an issue) #2

gervaisb opened this issue Jan 30, 2015 · 0 comments

Comments

@gervaisb
Copy link

Hi there,

Thanks a lot for your project, he give a clean MVP implementation for Android. However I miss an important part; the navigation.
Imagine that you allow users to add Character. You add a button to the list with a callback method to the presenter : onAddCharacterButtonClicked and then ?

  • A naive implementation can be to invoke a method on the view : setAddFormVisible(true) but then the view has to decide how you would present the form (display a view, open a dialog, move to another activity, ..). I dislike this solution.
  • The other solution is then to manage that from the presenter. He can decide to add a Fragment to the view, or open it as a dialog, or start another activity. But then you cannot test your presenter because he has direct dependencies to the Context or FragmentManager.
  • Some use an EventBus to send a message who is received by the Application or current Activity. But, again, I dislike the idea to use "system events" to handle navigation.
  • My current solution is to write a Navigator who decide what to do (dialog, activity, ..) and has everything to do it :
public interface Navigator {
  void moveTo(Class<?> target);
}

public class AndroidNavigator implements Navigator {
  private Activity activity = null;
  public void moveTo(Class<?> target) {
    if ( SomePresenter.class.equals(target) ) {
      new DialogFragmentWhoImplementSomePresenterView().show(activity.getFragementManager(), "..");
    } else if ( AnotherPresenter.class.equals(target) ) {
      activity.startActivity(new Intent(this, ActivityWhoImplementAnotherPresenterView.class));
    }
  }

  void setCurrentActivity(Activity activity) {
    this.activity = activity;
  }
}

public abstract class BaseActivity extends ActionBarActivity {
  @Inject Navigator navigator;
  public void onCreate(Bundle savedInstanceState) {
    // ...
    (AndroidNavigator) navigator).setCurrentActivity(this);
  }
}

So that I can test everything inside plain Junit tests. This is also the solution used in EffectiveAndroidUI

Do you have a better approach ? Did you plan to describe it in your project ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant