简体   繁体   中英

How to merge two application in one project?

Background
I have two android modules - A & B within a single project.
Each module has their own layouts

What I want to achieve
When I click a button in module A, it will start the main activity in module B and display the module B layout.

What I had tried - Not successful

  1. include module B in module A
  2. create Intent to start module B activity

Question
How can I achieve this? What are the possible ways?

ps: in the end I want one apk file .

Thank you very much for your time and assistance on this matter.

You need to use a connector to your other module by using a Navigator class. The Navigator should be the only way to access the activity inside the module. The Navigator is a singleton class .

You can create a Navigator class inside your library module (in your case, your module B) with something like this:

public class Navigator {
    private Navigator(){}

    public static Navigator getInstance() {
        return NavigatorHolder.INSTANCE;
    }

    private static class NavigatorHolder {
      private static final Navigator INSTANCE = new Navigator();
    }

    public void navigateToMainActivityB(Context context) {
      Intent intent = new Intent(context, MainActivity.class);
      context.startActivity(intent);
    }
}

Then you can launch the MainActivity of B module with:

Navigator.getInstance().navigateToMainActivityB(ModuleA_Activity.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM