rayman
rayman

Reputation: 21616

launch activities from different package

I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.

How can I do this? Any samples will be welcome.

this is what ive done, and the error i get:

first activity ("MainActivity") in a package: com.abelski.currencyclient and second activity("SecondActivity" in a diffrent package: com.idan.second

now i wanna call from MainActivity to SecondActivity.

ive added this line at the manifest of the MainActivity:

 <activity android:name="com.idan.second.SecondApplicationActivity"></activity>

now in main Activity i got this button which run this line:

Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class); 

and this is the rror:

04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):     

Upvotes: 15

Views: 51046

Answers (5)

masbayu
masbayu

Reputation: 71

First, you need to declare both packages and activities on Manifest :

        <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :

    <activity android:name=".second.SecondActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
        <intent-filter>
            <action android:name="android.intent.action.SECOND" />

            <category android:name="android.intent.category.SECOND" />
        </intent-filter>
    </activity>

then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :

public class MainActivity extends AppCompatActivity {

private Button mButtonSecond;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButtonSecond = findViewById(R.id.btn_second);

    mButtonSecond.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
            startActivity(newActivity);
        }
    });
   }
}

hope that can help, since i am not clear with the question structure

Upvotes: 0

user139992
user139992

Reputation:

I am assuming that by "packages" you mean applications.

We have: - ApplicationA with FirstActivity - ApplicationB with SecondActivity

If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:

<activity android:name=".SecondActivity">
  <intent-filter>
    <action android:name="applicationB.intent.action.Launch" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

You can create an Intent to launch this SecondActivity from the FirstActivity with:

Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);

What this all means is:

  • The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
  • When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it

The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html

Upvotes: 22

juejiang
juejiang

Reputation: 333

If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party App is simpler。With the first answer to that question, you don't need to make any modification to your second application.

Upvotes: 1

Niver
Niver

Reputation: 155

I had the same problem and the solution was another level in the root of your package name.

If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package">
...

    <activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

Your java code can create an intent and start the activity in the usual way:

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

Upvotes: 9

Samuh
Samuh

Reputation: 36484

Use explicit intents:

Intent intent = new Intent(context,ClassName.class);

where ClassName is from another package.

Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.

Upvotes: 0

Related Questions