how to start an activity in another module explicitly
Asked Answered
M

4

43

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.

<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 problem with implicit calling is that if i use this module in more than one app, both installed on user device it will show an app chooser dialog to user. So how can make this call explicit, preventing user from switching app?

this code will not run since HelloWorldActivity is not in the same package as calling activity

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

I really don't want to change my module for each project that uses it.

Montpellier answered 30/10, 2015 at 6:14 Comment(2)
Content Provider might helps you.Billings
Intent intent = new Intent(MainActivity.this, HelloWorldActivity.class);//use MainActivity.this instead of thisRegionalism
L
85

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();
    }
Laurynlausanne answered 1/6, 2017 at 11:13 Comment(2)
I have tested that defining as Activity in Manifest.xml fine is NOT necessary.Brunner
I was using Intent().setClassName(context, className) and this answer saved my day!Frivolity
P
5

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

try {
  Intent launchIntent =
      getPackageManager().getLaunchIntentForPackage("com.your.packagename");
  if (launchIntent != null) {
    startActivity(
        launchIntent); //null pointer check in case package name was not found ClassNotFoundException
  }
} catch (e) {
  e.printStackTrace();
}
Phonate answered 18/5, 2019 at 12:21 Comment(0)
A
1
Intent intent = new Intent();
intent.setClassName(context.getPackageName(), "ir.sibvas.testlibary1.HelloWorldActivity");
startActivity(intent);
Anallese answered 15/1, 2021 at 10:23 Comment(0)
K
0

If you want to remove hard-coding, you can follow my found method. I've used Hilt for this purpose. I used a Base Module to call the common the startActivity. I also used dependency injection to inject the generated class names to prevent the hardcode.

enter image description here

object AddressGenerator {
fun generateAddressList(): List<ActivityAddress> {
    val list = ArrayList<ActivityAddress>()
    ActivitiesNameEnum.values().forEach {
        val classAddresses = ActivityAddress(it, getClassName(it))
        list.add(classAddresses)
    }
    return list
}  
    private fun getClassName(classNameEnum: ActivitiesNameEnum) = when (classNameEnum) {
    ActivitiesNameEnum.FirstActivityEnum -> FirstActivity::class.java.name
    ActivitiesNameEnum.SecondActivityEnum -> SecondActivity::class.java.name
    ActivitiesNameEnum.ThirdActivityEnum -> ThirdActivity::class.java.name
}
}
Kao answered 29/10, 2023 at 16:19 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewBareback
@wohlstd You right The information is added.Kao
It looks better now. Although I admit I am not familiar with this subject. I got to your answer via the "low quality answers" review queue (BTW - the downvote is not mine).Bareback

© 2022 - 2024 — McMap. All rights reserved.