How to get started in another module explicitly

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

my manifest module looks like this.

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="ir.sibvas.testlibary1.HelloWorldActivity" android:label="@string/app_name" > <intent-filter> <action android:name="ir.sibvas.testlibary1.HelloWorldActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

Now I can start this activity from my project using this code

  Intent intent = new Intent("ir.sibvas.testlibary1.HelloWorldActivity"); startActivity(intent); 

but, as you can see, this code is implicit, and the problem with the implicit call is that if I use this module in more than one application, both installed on the user device, it will display the application selection dialog for the user. So, how to make this call explicit without letting the user switch the application?

this code will not work since HelloWorldActivity is not in the same package as call activity

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

I really do not want to change my module for every project that uses it.

+12
source share
3 answers

You can use Class.forName() , it worked for me when I needed to start work, which is in another module of my project.

  Intent intent = null; try { intent = new Intent(this, Class.forName("ir.sibvas.testlibary1.HelloWorldActivity")); startActivity(intent); } catch (ClassNotFoundException e) { e.printStackTrace(); } 
+25
source

The first start of module activity, then the start of the second module action and writing a line of code is fine.

  Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.your.packagename"); if (launchIntent != null) { startActivity(launchIntent);//null pointer check in case package name was not found } 
0
source

Explicit purpose:

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

should work fine if you added an import for HelloWorldActivity.class with the fully qualified package name of your module, namely. ir.sibvas.testlibary1.HelloWorldActivity

-4
source

All Articles