简体   繁体   中英

Start Activity from one package to another package

I have different activities in different packages,

Suppose I have two package ->com.pack1 ->com.pack2

I have a Activity in pack1 and want to call another Activity in pack2. How will I do that, using intents shows

21:19:10.405: W/System.err(7578): android.content.ActivityNotFoundException: Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml?

这是我的包裹的样子

    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(new ComponentName("com.mypkg.mystff","com.mypkg.mystuff.MyClass"));

The other answers are correct as well. This, however, will work from anywhere in the device.

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

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

Add this to the part of your xml

Please read the error.

"Have you declared this activity in your AndroidManifest.xml". pakage do not make problem For go one Activity another Activity. Check you AndroidManifest.xml that activity are declear in AndroidManifest.

I think problem happen for this reason.

thanks.

You need to make sure you declare the activity in you AndroidManifest.xml or you can't use it.

Example:

    <activity 
        android:name="com.yourpackage.YourActivity"
        android:launchMode="singleTop"
        android:configChanges="orientation"
     >
    </activity>

OK, so there are two parts that work together to define the Activity location. First, is the package attribute-- below it is `"com.flipagain2"

Also there is the android:name attribute, which, for example, is .FlipAgain2Activity. So, this means when Android looks for your activity, it is looking at

com.flipagain2.FlipAgain2Activity. Make sense? Just combine those two things.

So, you want Activities in two packages.

Change the second (one that isn't working" to look like this:

<activity android:name="com.package2.CaptureActivity" >
    <intent-filter>
        <action android:name="com.google.zxing.client.android.CaptureActivity" />
         <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Do you see what I changed? Leave the first Activity declaration the same, but change the second android:name= to match the fully qualified name including the package. Using this approach, every Activity can be in a different package.

Normally activities are private to their package. You can't start an activity in another package unless that activity is explicitly "exported" (ie: made known to other applications). Add this to you manifest for FlipAgain2Activity in com.pack2:

android:exported="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM