How to start an activity in an android Application

2020-06-06 06:14发布

I have an Android application in Android Studio. And I've added a library into the application. The button, view, and activities are defined in the library. When I click on the button, I need to navigate to the activity defined in the application.

Usually, to navigate to another page, we used the intent, like this:

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

But this is not a sufficient method to call the activity of the application from the library.

The problem is that the library and the application are independent; they have different packages. So the activity in the application cannot be recognized by the library.

How do I handle communication between the library and the application?

4条回答
你好瞎i
2楼-- · 2020-06-06 06:44

The normal way for doing this is to do this:

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

This is an explicit reference to an activity within your library. You should ensure that when starting this Activity that you catch the ActivityNotFoundException as this can happen when the Activity does not exist in the system.

Ideally when building this Intent you should insure that you can resolve it by using PackageManager APIs.

However you should try to avoid hardcoding packages, but when it comes to a library, sometimes you don't have a choice.

Also one thing to note is that within the library you need to ensure that the Activity is exported so that you can access it outside of your application.

android:exported

Whether or not the activity can be launched by components of other applications — "true" if it can be, and "false" if not. If "false", the activity can be launched only by components of the same application or applications with the same user ID. The default value depends on whether the activity contains intent filters. The absence of any filters means that the activity can be invoked only by specifying its exact class name. This implies that the activity is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the activity is intended for external use, so the default value is "true".

This attribute is not the only way to limit an activity's exposure to other applications. You can also use a permission to limit the external entities that can invoke the activity (see the permission attribute).

Ref

http://developer.android.com/guide/topics/manifest/activity-element.html

查看更多
我命由我不由天
3楼-- · 2020-06-06 06:44

Include the activity in AndroidManifest.xml.

To access an activity from any other project the easiest way is to pass the whole class name (including package, e.g; "com.myproject.MainActivitiy")

Calling from your library :

 Intent intent= new Intent("com.myproject.MainActivitiy");
 startActivity(intent);

And in your project manifest declare it like this

           <activity
            android:name="com.myproject.MainActivitiy"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.myproject.MainActivitiy" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
查看更多
贼婆χ
4楼-- · 2020-06-06 06:45

In this case, Intent can be used by providing the full class name including the package.

Let us suppose your current Activity class is MainActivity.java with package name com.app.myproject.

And you want to navigate to another Activity with class named Activity.java that is in another package com.app.external.

Include com.app.external.Activity.java in the manifest of your current project/library.

 <activity
        android:name="com.app.external.Activity"
        android:label="@string/title_activity_login"
        android:screenOrientation="portrait">
 </activity>

And your Intent should be like this -

Intent intent = new Intent(MainActivity.this, com.app.external.Activity.class);

startActivity(intent);
查看更多
劫难
5楼-- · 2020-06-06 06:54

In this case use Implicit Intent

Inside library Activity TESTActivity :

Intent intent = new Intent();
intent.setAction("com.myapp.myimplicit_action");
startActivity(intent);

and here is my manifest file declaration for some activity say 'ImplicitActivity' with the same action

<activity  android:name=".ImplicitActivity">
   <intent-filter>
   <action android:name="com.myapp.myimplicit_action" />
   <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</activity>
查看更多
登录 后发表回答