Question

I have 2 projects in Eclipse. When I click on a button in project1, I want it to launch an activity from project2. how can I do that ? i tried the following code

Intent intent = new Intent(this,com.project2.tp02.MainActivity.class);
intent.setClassName("com.project2.tp02", "com.project2.tp02.MainActivity.class");
startActivity(intent);  

I get an error message telling that first application stopped. How can I lauch it properly without such error?

Thanks in advance to any helper.


I got it work using this :

Intent intent = new Intent();
String pkg ="com.project2.tp02";
String clazz =pkg + ".MainActivity";
intent.setComponent(new ComponentName(pkg, clazz));
startActivity(intent);

I don't understand why that way it works and not previous one, if someone can explain I will be thankfull, but anyway I got my problem solved that way.

Was it helpful?

Solution

This is working code to invoke an activity which is in another project.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.my.package", "com.my.package.MyClass"));
startActivity(intent);

I think your first code did not work because you provide your current activity as context of invoked activity where it is in another application.

OTHER TIPS

            <intent-filter>
                <action android:name="com.project2.tp02.MainActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

Adding the above lines in the Manifest of your first activity might solve your problem.

It would be interesting what the error message is saying exactly... you do need the intent-filter as described by Swayam on the activity you want to call.

Try new Intent("com.project2.tp02.MainActivity"); to get the intent, instead of setting the class name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top