I structure my Android application following Uncle Bob's Clean Architecture which basically comprise of at least following relevant layers:-
- Presentation - Using MVVM approach. This layer contains Android's Activities and their corresponding ViewModels.
- Interactor - This layer encapsulates application's use cases logic and basically being called from several ViewModels to achieve business-specific tasks. It can also keep state that reflects the progress of a task.
Currently, there are a handful of use cases or workflows in my application. Each workflow will go through a specific sequence of pages (i.e. Activities or Fragments). As it has only few pages and workflows, it's simply just make sense to put navigation logic inside each ViewModel explicitly. Let's say there is a use case A that goes through following Activities:-
Use Case A
[HomeActivity] --> [ActivityA] --> [ActivityB]
Imagine that the ActivityA presents a user with some list of items for selection for which it will be acted upon in the ActivityB. In the ActivityA, I just simply call
startActivity(new Intent(this, ActivityB.class))
to navigate to ActivityB when user has done selecting an item and already called an interactor to keep state of which item being selected by a user.
...
Now the problem arises, a business needs a new use case B which draws a different workflow but reuses the same ActivityA in its workflow.
Use Case B
[HomeActivity] --> [ActivityA] --> [ActivityC]
As you can see, now the ActivityA can navigate to not just the ActivityB but also the ActivityC depending on which use case it's being participated in. While most of the logic in ActivityA as well as its view XML and its ViewModel is generic enough to be reused in both use cases, the navigation part is not.
How can I cope with this reusing issue so that the code in ActivityA and its related elements is generic enough to be reused in any workflow? What is the most elegant solution from your idea?