how to start an activity in another module explicitly

后端 未结 4 1775
囚心锁ツ
囚心锁ツ 2021-02-02 09:46

I created an aar and i added it to my project as an module. in this module i have a HelloWorldActivity that i want to run.

my module manifest looks like this.

相关标签:
4条回答
  • 2021-02-02 10:24

    You can use the Class.forName(), it worked for me when i was needed to start activity which is in another module in my project.

     Intent intent = null;
        try {
            intent = new Intent(this, 
               Class.forName("ir.sibvas.testlibary1.HelloWorldActivity"));
            startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2021-02-02 10:29
    Intent intent = new Intent();
    intent.setClassName(context.getPackageName(), "ir.sibvas.testlibary1.HelloWorldActivity");
    startActivity(intent);
    
    0 讨论(0)
  • 2021-02-02 10:43

    The explicit assignment:

    Intent intent = new Intent(this, HelloWorldActivity.class);
    startActivity(intent);
    

    should work fine provided you have added the import for HelloWorldActivity.class with the full package name of your module viz. ir.sibvas.testlibary1.HelloWorldActivity

    0 讨论(0)
  • 2021-02-02 10:44

    first module activity launch then second module activity launch and write a line of code is perfectly fine.

     Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.your.packagename");
                if (launchIntent != null) {
                    startActivity(launchIntent);//null pointer check in case package name was not found
                }
    
    0 讨论(0)
提交回复
热议问题