How do I implement navigation between android library modules focusing reusability and separation of concerns?

牧云@^-^@ 提交于 2020-05-28 04:51:06

问题


I'm trying to implement a separate navigation module to navigate between android library modules focusing scalability, reusability and module independence. My application architecture is similar to this example:

My current approach

1- Define NavigatorInterface for each library

2- Implement each NavigatorInterface in NavigationModule. (ofcourse navigation module will know about all other library modules but it does not matter as it won't be reused)

Following is the example code for my above mentioned architecture:

:auth

|-- LoginActivity

|-- SignupActivity

|-- AuthNavigator

public class LoginActivity extends AppCompatActivity {
    private NavigatorCoordinator navigator; // how do I achieve this injection, without using Dagger etc.

    .....
    private void signup(){
        navigator.NavigateToSignup(this);
    }

    private void profile(){
        navigator.NavigateToProfile(this);
    }
    .....
}


public class SignupActivity extends AppCompatActivity {
    private NavigatorCoordinator navigator; // how do I achieve this injection, without using Dagger etc.

    .....
    private void login(){
        navigator.NavigateToLogin(this);
    }

    private void profile(){
        navigator.NavigateToProfile(this);
    }
    .....
}



public interface AuthNavigator {
    void NavigateToLogin(Context context);
    void NavigateToRegister(Context context);
}

:profile

|-- ProfileActivity

|-- ProfileNavigator

public class ProfileActivity extends AppCompatActivity {
    private NavigatorCoordinator navigator; // how do I achieve this injection, without using Dagger etc.

    .....
    private void about(){
        navigator.NavigateToAbout(this);
    }
    .....
}


public interface ProfileNavigator {
    void NavigateToProfile(Context context);
}

:about

|-- AboutActivity

|-- AboutNavigator

public class AboutActivity extends AppCompatActivity {
    private NavigatorCoordinator navigator; // how do I achieve this injection, without using Dagger etc.

    .....
    private void profile(){
        navigator.NavigateToProfile(this);
    }
    .....
}


public interface AboutNavigator {
    void NavigateToAbout(Context context);
}

The above approach is a try to eliminate circular dependency within same module :auth as well as between two modules :profile & :about. Below is the implementation of :navigation.

:navigation

|-- Navigator

public class Navigator implements AuthNavigator, ProfileNavigator, AboutNavigator {
    @Override
    public void NavigateToLogin(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    @Override
    public void NavigateToSingup(Context context) {
        context.startActivity(new Intent(context, SignupActivity.class));
    }

    @Override
    public void NavigateToProfile(Context context) {
        context.startActivity(new Intent(context, ProfileActivity.class));
    }

    @Override
    public void NavigateToAbout(Context context) {
        context.startActivity(new Intent(context, AboutActivity.class));
    }
}

Questions:

1- I believe, I need to implement the NavigatorCoordinator in :navigation module, but how do I do it? Any example for this implementation?

2- How do I inject the dependency of NavigatorCoordinator in each Activity Class residing in different modules without using Deggar or any other framework? Can we do it by using Application class, if yes please give an example of implementation?

3- How do I setup the Launcher activity from any library, for example: LoginActivity?

4- How do I call the :navigation module from :app module in a way that it starts a specific activity, for example: ProfileActivity?

5- Is this a good inter-module navigation approach to achieve reusability, scalability and separation of concerns?

6- Are there any other similar and good approaches? Any code examples? Link to article?

PS: Please don't tell me to use Navigation Architecture Component.

来源:https://stackoverflow.com/questions/61359745/how-do-i-implement-navigation-between-android-library-modules-focusing-reusabili

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!